我在Angular 2中使用自定义指令来检测元素外的点击,但在Angular 4中不可能使用同样的方式。
[plunkr]
https://plnkr.co/edit/aKcZVQ?p=info
当我尝试在angular-4中使用相同的代码时,会出现以下错误:
1. Argument of type '{ template: string; directives: typeof ClickOutside[]; }' is not assignable to parameter of type 'Component'. ==>
@Component({
templateUrl: "",
directives: [ClickOutside]
})
2. Type 'Subscription' is not assignable to type 'Observable<MouseEvent>'. in the directive inside ngOnInit() and ngOnDestroy()
ngOnInit() {
this.globalClick = Observable
.fromEvent(document, 'click')
.delay(1)
.do(() => {
this.listening = true;
}).subscribe((event:MouseEvent) => {
this.onGlobalClick(event);
});
}
ngOnDestroy() {
this.globalClick.unsubscribe();
}
如果在 Angular 4 的指令声明中有任何更改,请告诉我,官方文档对此没有帮助。