在线观看不卡亚洲电影_亚洲妓女99综合网_91青青青亚洲娱乐在线观看_日韩无码高清综合久久

鍍金池/ 問答/HTML/ vue單頁面引入cdn

vue單頁面引入cdn

vue單頁面應(yīng)用中需要引入百度地圖的三個cdn文件來畫圖
但是網(wǎng)上都說在index.html加,這樣的話會有不必要的加載影響速度,
所以想問怎么只在單個組件里引入這三個文件

回答
編輯回答
挽歌

封裝一個工具函數(shù),比如bmap.js,大致代碼如下:

export function MP() {  
  return new Promise(function (resolve, reject) {  
    window.onload = function () {  
      resolve(BMap)  
    }  
    let script = document.createElement('script');  
    script.type = 'text/javascript';  
    script.src = 'http://api.map.baidu.com/api?v=2.0&ak=ak&callback=init';  
    script.onerror = reject;  
    document.head.appendChild(script);  
  })  
}  

組件中使用:

import { MP } from './bmap.js'
export default {
    mounted() {  
       this.$nextTick(function() {  
          const _this = this;  
          MP().then(BMap => {  
              // 其他操作          
          })
        })
    }
} 
2018年7月10日 04:54
編輯回答
清夢

看來你還是沒理解單頁面的含義,你的擔(dān)心是沒必要的,首頁引入并不會重復(fù)加載,建議關(guān)閉問題

2018年2月9日 02:29