前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >VUE页面实现加载外部HTML方法,困扰2天终于解决了

VUE页面实现加载外部HTML方法,困扰2天终于解决了

作者头像
Java架构师必看
发布2021-10-18 10:52:24
1.7K0
发布2021-10-18 10:52:24
举报
文章被收录于专栏:Java架构师必看

本文思路是把HTML请求以来,以v-html的形式加载到页面内部。注册全局组件【v-html-panel】

1.HtmlPanel.vue文件

代码语言:javascript
复制
<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

代码语言:javascript
复制
<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>

上一张效果图

VUE页面实现加载外部HTML方法,困扰2天终于解决了
VUE页面实现加载外部HTML方法,困扰2天终于解决了
猜您喜欢:
  1. vue项目工程中npm run dev 到底做了什么
  2. Vue中的join(),reverse()与 split()函数
  3. vscode如何调试vue代码
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 猜您喜欢:
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档