您是否必须取消订阅组件中的QueryList? [英] Do you have to unsubscribe from a QueryList in a component?
问题描述
使用@ContentChildren
或@ViewChildren
装饰器侦听DOM元素的更改时.我必须取消订阅QueryList
吗?
When using the @ContentChildren
or @ViewChildren
decorators to listen for changes to DOM elements. Do I have to unsubscribe from the QueryList
?
例如:
@Component({...})
export class ParentComponent implements AfterContentInit {
@ContentChildren(ChildComponent)
public children: QueryList<ChildComponent>;
public ngAfterContentInit(): void {
this.children.changes.subscribe(() => ....);
}
}
以上内容是否有问题?
已更新:
我要问的原因是我们不必取消订阅@Output
装饰器.这些组件在销毁后会自动取消订阅.
The reason I'm asking is that we don't have to unsubscribe to @Output
decorators. These are automatically unsubscribed by the component when it is destroyed.
我找不到任何说明与QueryList
相同的文档.
I can not find any documentation which says this is the same for the QueryList
.
推荐答案
您不必退订QueryList.它为您做到.
You don't have to unsubscribe from QueryList. It does it for you.
作为一般规则,当组件销毁后Observable仍然有效时,我将退订.在大多数情况下都可以使用.
As a general rule, I unsubscribe when Observable stays alive after Component destroyal. Works in most scenarios.
这篇关于您是否必须取消订阅组件中的QueryList?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!