您是否必须取消订阅组件中的QueryList? [英] Do you have to unsubscribe from a QueryList in a component?

查看:59
本文介绍了您是否必须取消订阅组件中的QueryList?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用@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.

请参阅此处: https://github.com/angular /angular/blob/7d137d7f8872a6fba72668e32f9baf2c5dcfc48b/packages/core/src/linker/query_list.ts#L115

作为一般规则,当组件销毁后Observable仍然有效时,我将退订.在大多数情况下都可以使用.

As a general rule, I unsubscribe when Observable stays alive after Component destroyal. Works in most scenarios.

这篇关于您是否必须取消订阅组件中的QueryList?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆