I have simple question. Do I need to unsubscribe manually or Angular is doing that for me or if does't how to understand what is happening?
Thanks.
I have simple question. Do I need to unsubscribe manually or Angular is doing that for me or if does't how to understand what is happening?
Thanks.
Im most cases - yes. You have to unsubscribe almost any time when you make a subscription. There are some exceptions when you don't have to do this though, like subscribing to http service.
Yes, You need to unsubscribe there are some exceptions like route
related observable.
You can find here different ways of unsubscribing https://blog.bitsrc.io/6-ways-to-unsubscribe-from-observables-in-angular-ab912819a78f
If you are using an async
pipe Angular handles the subscription, subscribing and unsubscribing for you within the component life cycle.
If you have subscribed within a component, you need to unsubscribe if the observable does not complete.
Here is a very SO post on the subject (pun intended!).
Yes, in many cases. If you don't do it it can lead to memory leakage. For example, it's normal to subscribe to an observable in ngOnInt()
and to unsubscribe in ngOnDestroy()
in the component.
As mentioned before it's not always you have to unsubscribe manually, for example when using some specific operators like take
. If you use async
in the template, manually unsubscribing is not necessary. Furthermore, it is also not necessary to unsubscribe Observables in services that are registered once.