asyncLoadScript(context,{src,id}){//异步加载script脚本 return new Promise(resolve=>{ if(context.dispatch("checkIsLoadScript",src)){ if($("#"+id) && $("#"+id).src==src){//若script标签存在,则先删除 console.log("删除成功") $("#"+id).remove(); } } let scriptNode = document.createElement("script"); scriptNode.setAttribute("type", "text/javascript"); scriptNode.setAttribute("charset", "utf-8"); scriptNode.setAttribute("id",id); scriptNode.setAttribute("src", src); document.body.appendChild(scriptNode); if (scriptNode.readyState) { //IE 判断 scriptNode.onreadystatechange = () => { if (scriptNode.readyState == "complete" || scriptNode.readyState == 'loaded') { resolve(); } } } else { scriptNode.onload = () => { console.log("script loaded"); resolve(); } } }) }, // 检测是否加载了 script脚本 文件 checkIsLoadScript(context,src) { let scriptObjs = document.getElementsByTagName('script'); let reg=RegExp(src); for(let sObj of scriptObjs) { if (sObj.src.match(reg)) { return true; } } return false; },
。