//放在 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(()=>{ // 這里我調用了騰訊地圖,所以這里掛載上調用地圖的方法就行 })