Search code examples
angularangular-routing

Unsubscribing from queryparammap - has this become necessary?


Googling around for subscription management in angular's ActivatedRoute comes up with the same reference to angular docs, which state that you do not have to unsubscribe from ActivatedRoute observables as it gets torn down by the router cleanly.

However, when viewing the docs themselves, it no longer has this section explaining this. I tried to use finalize to test if the observable was being completed when the component was being destroyed:

this.activatedRoute.queryParamMap.pipe(finalize(() => console.log('done')).subscribe()

However, the console log was never being called. I was not sure if this was because the observable was just straight up being destroyed, or was just remaining open.

Has it become necessary to manage the subscription lifecycle for ActivatedRoute?


Solution

  • Each ActivatedRoute is bound to a routed component and when a route changes occurs, the current component being displayed is going to be destroyed, as well as its bound ActivatedRoute, so that's why you won't get a complete notification.

    I've elaborated a bit on it in this answer.