原生html使用 vue 组件

html-css09

原生html使用 vue 组件,第1张

以下纯个人理解 欢迎 大佬指正

1.【vue 2.0】

http-vue-loader.js

原生html需要引入以上js

// 使用httpVueLoader

2.【vue 3.0】

组件的理解可以在各个js中 当函数使用

function fun1(id){

const Counter = {

data() {

return {

counter: "反转字符"

}

},

components:{

// "button-counter":app1

},

mounted(){

this.changeData()

}

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文件内部的相对路径将不会被自动识别,绝对路径可以。