全局引用
在App.vue文件中添加如下代碼
這樣在全局所有頁面中都可以直接使用該外部js中的函數
onLaunch: function() {var script = document.createElement('script');script.src = "https://www.test.com/api/testapi.js";document.body.appendChild(script);
},
單頁面引用
如果只需要在單頁面中引用外部js文件,導入方式如下代碼所示
export default {mounted() {const script = document.createElement('script');script.type = 'text/javascript';script.src = 'https://www.test.com/api/testapi.js'; // JS文件的相對或絕對路徑document.body.appendChild(script);script.onload = () => {console.log("External JavaScript loaded successfully.");// 調用外部JS文件中的函數等操作externalFunction();};},
};