vue 动态引入 js 文件,局部引入

//放在 script 底下

function loadTcScript(cb) {
  loadScript(cb, {
    id: 'tcPlayerScriptId',
    url: '//map.qq.com/api/js?v=2.exp&key="',
  });
}

function loadScript(cb, obj) {
  if (document.getElementById(obj.id)) {
    cb();
  } else {
    const script = document.createElement('script');
    script.async = true;
    script.src = obj.url;
    script.id = obj.id;
    script.onload = () => {
        cb();
    };
    document.body.appendChild(script);
  }
}

 

// 带着 min.js 之类的文件是可以直接引入,不需要动态挂载

<script src="https://3gimg.qq.com/lightmap/components/geolocation/geolocation.min.js"></script>

 

 

//挂载在 mounted 里面,注意:不用加 this 去指向

loadTcScript(()=>{
         // 这里我调用了腾讯地图,所以这里挂载上调用地图的方法就行
    })

 

posted @ 2021-12-10 09:55  薛定谔_猫  阅读(2526)  评论(0)    收藏  举报