方法定义:
loadScript(src, callback) { var script = document.createElement('script'), head = document.getElementsByTagName('head')[0]; script.type = 'text/javascript'; script.charset = 'UTF-8'; script.src = src; if (script.addEventListener) { script.addEventListener('load', function () { callback(); }, false); } else if (script.attachEvent) { script.attachEvent('onreadystatechange', function () { var target = window.event.srcElement; if (target.readyState == 'loaded') { callback(); } }); } head.appendChild(script); }
调用:
var that=this;//监听函数中的this不再指向组件本身,而是指向事件监听者,所以需要提前保存this指针
this.loadScript('xxx.js',function(){ console.log('加载完成'); });