vue使用iframe嵌入網(wǎng)頁的示例代碼
1、列表頁面:
this.$router.push({ name: 'userTemplate', params: { reportUrl: reportUrl, reportType: reportType }})
點擊查看后觸發(fā)事件,帶入?yún)?shù)跳轉(zhuǎn)到userTemplate頁面;reportType有兩種類型,0表示reportUrl是絕對網(wǎng)址,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-寬高自適應(yīng)顯示
*/
function changeMobsfIframe() {
const mobsf = document.getElementById('child')
const deviceWidth = document.body.clientWidth
const deviceHeight = document.body.clientHeight
mobsf.style.width = (Number(deviceWidth) - 30) + 'px' // 數(shù)字是頁面布局寬度差值
mobsf.style.height = (Number(deviceHeight) - 80) + 'px' // 數(shù)字是頁面布局高度差
}
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("讀取異常");
}
}
到此這篇關(guān)于vue使用iframe嵌入網(wǎng)頁的示例代碼的文章就介紹到這了,更多相關(guān)vue使用iframe嵌入網(wǎng)頁內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue3中安裝并使用CSS預(yù)處理器Sass的方法詳解
Sass是一種CSS預(yù)處理器,它擴展了CSS的功能,提供了更高級的語法和特性,例如變量、嵌套、混合、繼承和顏色功能等,這些特性可以幫助開發(fā)者更高效地管理和維護樣式表,本文介紹vue3中安裝并使用CSS預(yù)處理器Sass的方法,感興趣的朋友一起看看吧2024-01-01
uniapp中使用u-loadmore,loadText內(nèi)容不隨status改變刷新方式
這篇文章主要介紹了uniapp中使用u-loadmore,loadText內(nèi)容不隨status改變刷新方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-05-05
vue 實現(xiàn)axios攔截、頁面跳轉(zhuǎn)和token 驗證
這篇文章主要介紹了vue 實現(xiàn)axios攔截、頁面跳轉(zhuǎn)和token 驗證,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-07-07

