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