前后端分離外部網(wǎng)站同意加載不行,后端提供了接口。但有一部分?jǐn)?shù)據(jù),比較產(chǎn)品說明文件,是存在其他的服務(wù)器上的。所以,在頁面顯示的時(shí)候,如果以頁面內(nèi)嵌的形式顯示這個(gè)說明文件。需要搞點(diǎn)事情以達(dá)到想要的效果。本文主要和大家介紹VUE頁面中加載外部HTML的示例代碼,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧,希望能幫助到大家。
不同以往的標(biāo)簽外部網(wǎng)站同意加載不行,那種方式比較Low,另外有其他的一些BUG。
本文思路是把HTML請(qǐng)求以來,以v-html的形式加載到頁面內(nèi)部。注冊(cè)全局組件【v-html-panel】
1..vue文件
<script> export default{ // 使用時(shí)請(qǐng)使用 :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>
.vue
<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>
上一張效果圖
注意事項(xiàng):
NGINX跨域配置:
(如果使用*的話,好像會(huì)出錯(cuò),這里直接使用請(qǐng)求源的地址,如果擔(dān)心安全性的話,可以用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; ... }