//await
await improt('').default
//js文件添加在script标签中,加载完毕返回结果
const createScriptLoaded = (url) => {
return new Promise((resolve) => {
const script = document.createElement("script")
script.src = url
script.onload = function() {
resolve()
}
document.body.appendChild(script)
const link = document.createElement("link")
link.setAttribute('rel','stylesheet')
link.setAttribute('type','text/css')
link.setAttribute('href',url)
script.onload = function() {
resolve()
}
document.querySelector('head').appendChild(link)
})
}
const loadedScripts = []
const loadScripts = (urls) => {
//是否重复资源
const diffUrls = urls.filter((item) => loadedScripts.indexOf(item) < 0)
loadedScripts.push(...diffUrls)
return new Promise((resolve) => {
function doLoad(url) {
createScriptLoaded(url).then(() => {
if (diffUrls.length > 0) {
doLoad(diffUrls.shift())
} else {
resolve
}
})
}
//循环加载js文件,数组导出
if (diffUrls.length > 0) {
doLoad(diffUrls.shift())
} else {
resolve
}
})
}
export default loadScripts
loadScripts(['']).then(()=>{
})