export function loadScript(url) {
let isLoaded = false;
return new Promise((resolve,reject) => {
if(isLoaded) {
resolve();
true;
}
let script = document.createElement('script');
script.type = 'text/javascript';
script.charset = 'utf-8';
script.async = ;
script.src = url;
script.onerror = reject;
script.onload = () {
isLoaded = ;
resolve();
}
document.head.appendChild(script);
})
}
sum(a,b) {
return a + b;
}
<template>
div>
其他组件内容
</>
>
script>
import {loadScript} from '@/utils/index.js;
export default {
data() {
return {
}
},created() {
loadScript(./static/lib.js).then(() => {
console.log(sum(2,3))
})
},methods: {
}
}
style>
script type="text/javascript" src="./static/lib.js"></>