前段时间我问过这个问题,关于将 jquery 事件委托给 typescript 类中的事件处理程序方法。答案是使用以下模式
public class LoginDialog {
...
constructor() {
//this.open = this.OpenHandler; //Incorrect way
this.open = (event: Event, ui: DialogUIParams) => { //Correct way
this.OpenHandler(this, event, ui);
};
...
}
public OpenHandler(context: LoginDialog, event: Event, ui: DialogUIParams) {
//Use context as "this"
}
...
}
但是,现在我处于类似的情况,我需要在将来的某个时间删除处理程序,但由于匿名函数而无法删除:
public class LoginDialog {
...
constructor() {
this.open = (event: Event, ui: DialogUIParams) => {
this.OpenHandler(this, event, ui);
};
this.close = (event: Event, ui: DialogUIParams) => {
this.CloseHandler(this, event, ui);
}
...
}
public OpenHandler(context: LoginDialog, event: Event, ui: DialogUIParams) {
$(window).on("scroll", () => { context.ScrollHandler(context); });
//$(window).on("scroll", context.ScrollHandler(context)); //Never gets called
//$(window).on("scroll", context.ScrollHandler); //this = window
}
public CloseHandler(context: LoginDialog, event: Event, ui: DialogUIParams) {
$(window).off("scroll", () => { context.ScrollHandler(context); }); //Does not remove
}
public ScrollHandler(context: Interfaces.IBaseDialog) {
context.jQueryDialog.dialog("option", "position", "center");
...
}
...
}
在这种情况下,on
绑定中没有箭头语法,我ResizeOrScrollHandler
永远不会被调用,但是我无法删除 CloseHandler 中的事件处理程序