gpt4 book ai didi

Angular 是否有必要取消订阅 this.activatedRoute 订阅

转载 作者:行者123 更新时间:2023-12-05 00:48:20 28 4
gpt4 key购买 nike

我的代码:

ngOnInit() {

this.activatedRoute.params.subscribe((params: Params) => {
// do stuff
})

this.activatedRoute.data.subscribe((data: any) => {
// do stuff
})

}

我的问题是......我需要在 ngOnDestroy 中取消订阅这些吗?

出于习惯,我总是取消订阅,但不确定是否有必要。

谢谢

最佳答案

不,您不需要取消订阅路由可观察对象

来自 Documentation

When subscribing to an observable in a component, you almost always arrange to unsubscribe when the component is destroyed.

There are a few exceptional observables where this is not necessary. The ActivatedRoute observables are among the exceptions.

The ActivatedRoute and its observables are insulated from the Router itself. The Router destroys a routed component when it is no longer needed and the injected ActivatedRoute dies with it.

Feel free to unsubscribe anyway. It is harmless and never a bad practice.

关于Angular 是否有必要取消订阅 this.activatedRoute 订阅,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52006017/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com