如何动态引入js
try {
await loadJs('https://xxxxxx.js')
console.log(' 加载成功 ')
} catch {
console.log(' 加载失败 ')
}
function loadJs(path) {
if (document.head.querySelector(`script[src="${path}"]`) != null) return
var script = document.createElement('script')
script.src = path
script.type = 'text/javascript'
document.head.appendChild(script)
return new Promise((res, rej) => {
script.addEventListener('load', res)
script.addEventListener('error', rej)
})
}