class ScriptLoader {
private loadedScripts : Array<string> = new Array<string>();
constructor(private head = document.getElementsByTagName('head')[0], ) {
if (typeof(head) == "undefined")
alert("A página não contém o elemento head.");
}
load(scripts: Array<string>) {
scripts.forEach(element => {
let node = document.createElement('script') as HTMLScriptElement;
node.type = 'text/javascript';
node.charset = 'utf-8';
node.async = true;
node.addEventListener('load', (el: HTMLScriptElement, ev: Event) => this.onLoad(el, ev), false);
node.src = element;
this.head.appendChild(node);
});
}
onLoad(el: HTMLScriptElement, ev: Event) : any {
this.loadedScripts.push(el.src)
}
}
错误消息:
没有重载匹配此调用。 重载1,共2个,'(类型:“负载”,侦听器:(此:HTMLScriptElement,ev:事件)=>任意,选项?:布尔| AddEventListenerOptions |未定义):无效”,出现以下错误。 类型'(el:HTMLScriptElement,ev:Event)=> any'的参数不能分配给类型'(this:HTMLScriptElement,ev:Event)=> any'的参数。 重载2之2,'((类型:字符串,侦听器:EventListenerOrEventListenerObject,选项?:布尔值| AddEventListenerOptions |未定义):无效),出现以下错误。 类型'(el:HTMLScriptElement,ev:Event)=> any'的参数不能分配给'EventListenerOrEventListenerObject'类型的参数。 类型'((el:HTMLScriptElement,ev:事件)=>任何'都不能分配给类型'EventListener'。
答案 0 :(得分:0)
class ScriptLoader {
private loadedScripts : Array<string> = new Array<string>();
constructor(private head = document.getElementsByTagName('head')[0], ) {
if (typeof(head) == "undefined")
alert("A página não contém o elemento head.");
}
load(scripts: Array<string>) {
scripts.forEach(element => {
let node = document.createElement('script') as HTMLScriptElement;
node.type = 'text/javascript';
node.charset = 'utf-8';
node.async = true;
// fix
node.addEventListener('load', () => this.onLoad(node), false);
node.src = element;
this.head.appendChild(node);
});
}
onLoad(el: HTMLScriptElement/*, ev: Event*/) : any {
this.loadedScripts.push(el.src)
}
}