
1、新建html文档,然后在head标签中引入vue的js文件,这里以引入cdn文件为例。2、在body标签中添加一个id为“app”的div标签,然后在这个div标签中再添加一个div标签,在vue中添加一个变量数据,然后用“{{}}”符号将变量引入到div标签中,并且在标签中添加“v-html="变量"v-html="变量"”属性。3、用浏览器打开html文档,这时可以在页面中看到被v-html指令添加的html元素。
.路由传值 路由对象如下图所示: 在跳转页面的时候,在js代码中的 *** 作如下,在标签中使用标签this.$router.push({ name: 'routePage', query/params: { routeParams: params } }) 需要注意的是,实用params去传值的时候,在页面刷新时,参数会消失,用query则不会有这个问题。 这样使用起来很方便,但url会变得很长,而且如果不是使用路由跳转的界面无法使用。
2.
通过$parent,$chlidren等方法调取用层级关系的组件内的数据和方法 通过下面的方法调用:this.$parent.$data.id //获取父元素data中的id
1、HtmlPanel.vue文件
<template> <div> <mu-circular-progress :size="40" v-if="loading"/> <div v-html="html"></div> </div></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
?
12345678910111213141516171819202122232425
<template> <div> <v-html-panel :url.asyc="url1"></v-html-panel> <v-html-panel :url.asyc="url2"></v-html-panel> </div></template><style scoped> div{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>
2、效果图
3、注意事项:
直接使用axios处理的GET请求,需要处理跨域;
外部的css样式会作用到显示的html;
同时加载的外部html里的script也可能会执行,需要按需处理下;
外部HTML文件内部的相对路径将不会被自动识别,绝对路径可以。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)