我试图让这个功能“this.kill()”起作用。但当我调用它时,我得到这个错误:“TypeError:this.kill不是一个函数”,我无法找到解决它的方法。
HTML:
<div class="message success" id="alert"></div>
基类:
export class BaseMessage {
constructor(protected element:HTMLElement, protected message:string) {
this.init();
}
init():void {
this.setText(this.message);
this.addListeners();
}
kill():void {
this.removeListeners();
}
setText(message:string):void {
this.element.textContent = message;
}
addListeners():void {
this.element.addEventListener('click',this.onClick);
}
removeListeners():void {
this.element.removeEventListener('click',this.onClick);
}
onClick():void {
this.kill();
//its this that causes the error
}
}
消息类
import { BaseMessage } from './basemessage';
export class Message extends BaseMessage {
constructor(element:HTMLElement, message:string) {
super(element, message);
}
}
主要
import { Message } from './components/message';
const mess = document.getElementById('alert');
const message = new Message(mess,'Tack, tack!');