vue使用iframe嵌入网页的示例代码
作者:w_t_y_y 发布时间:2024-05-05 09:12:04
标签:vue,iframe,嵌入网页
1、列表页面:
this.$router.push({ name: 'userTemplate', params: { reportUrl: reportUrl, reportType: reportType }})
点击查看后触发事件,带入参数跳转到userTemplate页面;reportType有两种类型,0表示reportUrl是绝对网址,1表示reportUrl是本地html文件。
2、userTemplate页面:
<template>
<div>
<iframe v-if="reportType==0" name = "child" id = "child" v-bind:src="reportUrl"
width="auto" height="300"
frameborder="0" scrolling="no" style="position:absolute;top:80px;left: 30px;"
></iframe>
<div v-if="reportType==1" v-html="htmlContent"
width="auto" height="300" scrolling="no" style="position:absolute;top:80px;left: 30px;"></div>
</div>
</template>
<script>
import {
getFile
} from '@/api/report'
export default {
mounted() {
/**
* iframe-宽高自适应显示
*/
function changeMobsfIframe() {
const mobsf = document.getElementById('child')
const deviceWidth = document.body.clientWidth
const deviceHeight = document.body.clientHeight
mobsf.style.width = (Number(deviceWidth) - 30) + 'px' // 数字是页面布局宽度差值
mobsf.style.height = (Number(deviceHeight) - 80) + 'px' // 数字是页面布局高度差
}
changeMobsfIframe()
window.onresize = function() {
changeMobsfIframe()
}
},
data() {
return {
htmlContent: '',
reportUrl: '',
reportType: ''
}
},
created() {
// this.fileName = '../static/file/' + this.$route.params.report_url
this.reportUrl = this.$route.params.reportUrl
this.reportType = this.$route.params.reportType
if (this.reportType == 1) {
getFile(this.reportUrl).then(res => {
if (res.code === 200) {
this.htmlContent = res.data
}
})
}
}
}
</script>
<style rel="stylesheet/scss" lang="scss" scoped>
</style>
后端getFile:
//读取文件
@RequestMapping("/getFile")
@ResponseBody
public HttpResult getFile(String reportUrl){
StringBuilder result = new StringBuilder();
try{
FileSystemResource resource = new FileSystemResource("D:"+File.separator+"test"+File.separator+reportUrl);
File file = resource.getFile();
BufferedReader br = new BufferedReader(new FileReader(file));
String s = null;
while((s = br.readLine())!=null){
result.append(System.lineSeparator()+s);
}
br.close();
return HttpResult.getSuccessInstance(result);
}catch(Exception e){
e.printStackTrace();
return HttpResult.getFailedInstance("读取异常");
}
}
来源:https://blog.csdn.net/w_t_y_y/article/details/84100658


猜你喜欢
- 本文实例讲述了Python使用lambda表达式对字典排序操作。分享给大家供大家参考,具体如下:lambda表达式也常用于字典排序,既然写到
- 这几天一直在看《Pro JavaScript Techniques》,书中有不少优美、健壮代码,让我不得不惊叹老外对语言这东西的研究程度之深
- 小编相信对于大多数熟悉Python的人来说,一定都听说并且使用过pip这个工具,但是对它的了解可能还不一定是非常的透彻,今天小编就来为大家介
- 永远不要相信用户输入的内容具有适当的大小或者包含适当的字符。在使用其做出决策之前应该始终对用户输入进行验证。最佳的选择是创建一个
- VueUse官方链接一、什么是VueUseVueUse不是Vue.use,它是为Vue 2和3服务的一套Vue Composition AP
- 我从Stephen A. Goss那读到关于了《Python 3正在毁灭Python》。这篇文章有不少精彩的论点,但我却并不认为Python
- 本文实例讲述python调用Moxa PCOMM Lite通过串口Ymodem协议实现发送文件的方法,该程序采用python 2.7编写。主
- 取得 Oracle 10g 安装程序,或从 Oracle 技术网(OTN)下载光盘映像。在评估阶段您可以免费下载和使用无技术限制的全功能 O
- 前言后续还会更新更多优雅的规范。命名风格1. 【强制】代码中的命名均不能以下划线或美元符号开始,也不能以下划线或美元符号结束。&n
- WebStorm 遇到的问题总结最近自己WebStorm用得多一点了和android studio一样,同样是IDEA的ide(记得自己还是
- Python3 线程中常用的两个模块为:_threadthreading(推荐使用)使用Thread类创建import threadingf
- 本文实例讲述了python中sleep函数用法。分享给大家供大家参考。具体如下:Python中的sleep用来暂停线程执行,单位为秒#---
- 1. 环境准备1.1 安装pillow 和 pytesseractpython模块库需要 pillow 和 pytesseract 这两个库
- 2009年 6月6日,空间4岁啦!与此同时Qzone月登录用户2亿,同时在线用户也突破了1000万。这是让人欢欣雀跃的数字,在空间同事眼里,
- 本文实例讲述了python简单的函数定义和用法。分享给大家供大家参考。具体分析如下:这里定义了一个温度转换的函数及其用法。def conve
- 本文实例讲述了Python利用正则表达式匹配并截取指定子串及去重的方法。分享给大家供大家参考。具体如下:import repattern=r
- 1. 什么是XSLT 大家可能听说过XSL(eXtensible Stylesheet Language),XSL和我们这里说的XSLT从狭
- 前言本文是美团一位大佬写的,还不错拿出来和大家分享下,代码中嵌套在html中sql语句是java框架的写法,理解其sql要执行的语句即可。背
- 本文实例讲述了Python3.6日志Logging模块简单用法。分享给大家供大家参考,具体如下:Logging是一个很方便的模块,用来打印日
- 大家都知道Js有同源策略,就是主域名不同嵌套的iframe不允许Js通信的。比如我有一个网站,网站中要嵌入其网站的页面。那么我可