不同以往的iframe标签,那种方式比较low,另外有其他的一些bug。
本文思路是把html请求以来,以v-html的形式加载到页面内部。注册全局组件【v-html-panel】
1.htmlpanel.vue文件
<template> <p> <mu-circular-progress :size="40" v-if="loading"/> <p v-html="html"></p> </p> </template> <style> </style> <script> export default{ // 使用时请使用 :url.sync=""传值 props: { url: { required: true } }, data () { return { loading: false, html: '' } }, watch: { url (value) { this.load(value) } }, mounted () { this.load(this.url) }, methods: { load (url) { if (url && url.length > 0) { // 加载中 this.loading = true let param = { accept: 'text/html, text/plain' } this.$http.get(url, param).then((response) => { this.loading = false // 处理html显示 this.html = response.data }).catch(() => { this.loading = false this.html = '加载失败' }) } } } } </script>
htmlviewsample.vue
<template> <p> <v-html-panel :url.asyc="url1"></v-html-panel> <v-html-panel :url.asyc="url2"></v-html-panel> </p> </template> <style scoped> p{color:red} </style> <script> export default{ data () { return { url1: '', url2: '' } }, mounted () { this.url1 = 'http://file.xxx.com/group1/m00/0c/f5/xxxxxxxx.html' this.url2 = 'http://file.xxx.com/group1/m00/0d/3b/yyyyyyy.html' }, methods: { } } </script>
上一张效果图
注意事项:
直接使用axios处理的get请求,需要处理跨域
外部的css样式会作用到显示的html
同时加载的外部html里的script也可能会执行,需要按需处理下
外部html文件内部的相对路径将不会被自动识别,绝对路径可以
nginx跨域配置:
(origin如果使用*的话,好像会出错,这里直接使用请求源的地址,如果担心安全性的话,可以用if+正则条件判断下)
location / { add_header access-control-allow-origin $http_origin; add_header access-control-allow-credentials true; add_header access-control-allow-methods get; access_log /data/nginx/logs/fdfs_https.log main; ... }
相关推荐:
html悬浮框架的iframe加载html设置使用示例
以上就是vue页面加载外部html实例详解的详细内容。
