0
我正在動態創建component
並訂閱其中的一個@Ouput
EventEmitter
。Angular 2:動態組件的Event Emitter訂閱,需要退訂嗎?
相關的代碼:
動態組件:
export class DynamicComponent implements OnInit {
@Output() results: EventEmitter<any> = new EventEmitter<any>();
...
}
父組件:
private loadDynamicComponent(): void {
const componentFactory = this.factoryResolver.resolveComponentFactory(DynamicComponent);
const componentRef = this.host.viewContainerRef.createComponent(componentFactory);
(<DynamicComponent>componentRef.instance).results.subscribe(result => {
this.result = result;
})
}
我感到困惑的我是否需要unsubscribe
或將Angular
打掃一下嗎?
我很驚訝你甚至得到了任何結果。我不認爲你實際上可以訂閱活動。你確定你的實施是正確的嗎? – unitario
是的,它工作。經過一番挖掘,我發現這是處理動態組件的@Output()方法。 – Thibs
好的。沒有必要退出事件或承諾,因爲他們執行一次,然後他們隨風飄逝。你只需要關心什麼時使用Observables。 – unitario