Do I have to unsubscribe from ActivatedRoute (e.g. params) observables?

前端 未结 5 877
既然无缘
既然无缘 2020-12-01 10:17

I find many examples where ActivatedRoute Observables like params or url are subscribed but not unsubscribed.

construc         


        
5条回答
  •  误落风尘
    2020-12-01 10:30

    From the docs :

    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.

提交回复
热议问题