Angular有必要退订this.activatedRoute订阅 [英] Angular is it necessary to unsubscribe from this.activatedRoute subscriptions

查看:113
本文介绍了Angular有必要退订this.activatedRoute订阅的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的代码:

ngOnInit() {

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

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

}

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

My question is ... do I need to unsubscribe from these in ngOnDestroy?

出于习惯,我总是退缩,但不确定在这里是否有必要.

As a matter of habit I always unsub but not sure if it is necessary here.

谢谢

推荐答案

不,您不需要取消订阅可观测路线

No, you don't need to unsubscribe from route observables

> 文档

订阅组件中的可观察对象时,您几乎总是安排在组件销毁时退订.

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

有一些不需要观察的例外观察点.可观察到的ActivatedRoute除外.

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

ActivatedRoute及其可观察物与路由器绝缘本身.当路由组件不再存在时,它会销毁它需要,注入的ActivatedRoute随之死亡.

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订阅的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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