如果我们有一个动态(动态创建 - 非声明式)ComponentRef
实例,并且我们在该实例上调用 destroy()
,是否会取消对 的任何订阅已订阅的 >EventEmitter
实例。
例如,如果我们有一个 output
EventEmitter
并且我们像这样订阅它:
this.componentRef.instance.output.subscribe(event => console.log(event));
我们调用 componentRef.destroy()
来取消对 output
EventEmitter
的订阅?
包含答案的摘要文章
https://medium.com/@ole.ersoy/subscribing-to-dynamic-component-eventemitters-4f931a5013e3
最佳答案
当调用 subscribe 方法时,会返回一个订阅对象。如果我跟踪那个物体。每当 Angular 销毁组件时,您都必须可以调用取消订阅。
例如:
ngOnDestroy() {
this.sub.unsubscribe();
}
它不会取消订阅 ngOnDestroy()
关于javascript - ComponentRef.destroy() 方法是否也会取消订阅组件事件发射器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54140421/