是否可以在 ngOnDestroy 上发出自定义事件?我尝试过,但似乎不起作用...我基本上需要知道指令何时从 UI 中删除。
@Output() rowInit = new EventEmitter();
@Output() rowDestroy = new EventEmitter();
ngAfterViewInit() {
this.rowInit.emit(this);
}
ngOnDestroy() {
console.log("I get called, but not emit :(, ngAfterViewInit works :)");
this.rowDestroy.emit(this);
}
我认为你可以使用EventEmitter
在服务中而不是在组件本身中定义。这样,您的组件将利用服务的此属性来发出事件。
import {EventEmitter} from 'angular2/core';
export class NotificationService {
onDestroyEvent: EventEmitter<string> = new EventEmitter();
constructor() {}
}
export class MyComponent implements OnDestroy {
constructor(service:NotificationService) {
this.service = service;
}
ngOnDestroy() {
this.service.onDestroyEvent.emit('component destroyed');
}
}
其他元素/组件可以订阅此 EventEmitter 以获得通知:
this.service.onDestroyEvent.subscribe(data => { ... });
希望对你有帮助,
蒂埃里
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)