Angular 角度2:动态组件';s事件发射器订阅,需要取消订阅吗?
我正在动态创建一个Angular 角度2:动态组件';s事件发射器订阅,需要取消订阅吗?,angular,angular2-template,Angular,Angular2 Template,我正在动态创建一个组件,并订阅其@output事件发射器 有关守则: 动态组件: export class DynamicComponent implements OnInit { @Output() results: EventEmitter<any> = new EventEmitter<any>(); ... } private loadDynamicComponent(): void { const componentFactory = t
组件
,并订阅其@output
事件发射器
有关守则:
动态组件:
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;
})
}
导出类DynamicComponent实现OnInit{
@Output()结果:EventEmitter=neweventemitter();
...
}
父组件:
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;
})
}
private loadDynamicComponent():void{
const componentFactory=此.factoryResolver.resolveComponentFactory(DynamicComponent);
const componentRef=this.host.viewContainerRef.createComponent(componentFactory);
(componentRef.instance).results.subscribe(结果=>{
this.result=结果;
})
}
我不知道我是否需要取消订阅,或者我是否会把它清理干净?我不确定你是否需要取消订阅,但以防万一,我会用(1)来完成观察
confirmDialogRef.instance.buttonClicked
.take(1)
.subscribe(val => {
this.appRef.detachView(confirmDialogRef.hostView);
resolve(val);
});
我很惊讶你竟然得到任何结果。我不认为你真的可以订阅活动。你确定你的实现是正确的吗?是的,它正在工作。经过一番挖掘,我发现这是处理动态组件的@output()的方法。没有必要退订活动或承诺,因为它们只执行一次,然后就随风而逝。你只需要关心在使用可观察对象时会发生什么。实际上,这是一个可观察对象,它永远不会完成……你只需要在调用OnInit时得到结果?在这种情况下,为什么不使用first()操作符并获取最先出现的值呢?