export function loadScript (url: string[]) { // 如果是数组 const promiseList: Array<Promise<any>> = [] url.forEach((item) => { const p = new Promise((resolve, reject) => { const script: any = document.createElement('script') script.type = 'text/javascript' script.src = item script.onload = () => { script.onload = null script.onerror = null resolve() } script.onerror = () => { script.onload = null script.onerror = null reject() } const head = document.getElementsByTagName('head')[0]; (head || document.body).appendChild(script) }) promiseList.push(p) }) return Promise.all(promiseList) }
使用方法:
loadScript([ //融云库中的XMLHttpRequest判断在IOS9中有问题IOS9中为object,融云判断的是否为function。2019.10.18 '/js/libs/RongIMLib-2.3.5.min.js?v2' ]).then(() => { loadScript([ '/js/libs/RongEmoji-2.2.7.min.js', '/js/libs/Libamr-2.2.5.min.js', '/js/libs/RongIMVoice-2.2.6.js' ]).then(async () => { // do something }) })