As we know multiple components share the common data and always need updated shared data. In such scenarios most of the time BehaviorSubject is use...
Some comments have been hidden by the post's author - find out more
For further actions, you may consider blocking this person and/or reporting abuse
Everything is awesome but there is one thing you should pay attention.
By subscribing inside
ngOnInit
you are introducing a memory leak since this hook will be triggered on component initialization.For example going to page
/
and then/orders
for example. Go back and forward a few times and print out console.log inside the subscription. You should get a multiple console log printing instead of one. This is because you are not unsubscribing from the observable.One way to mitigate this issue is by letting html template to subscribe for you using
async
pipeInside the
ngOnInit
you just need to define the observableIn some cases when you need to subscribe for example to router change event you don't need to subscribe inside the template so you can use a different approach
Cheers!
You maybe interested to unsubscribe using takeUntil instead of saving the subscription and manually unsubscribing (you can have multiple subscriptions and it's not that convenient to manually unsubscribe from all of them).
Can you please post an article explaining how to use "TakeUntil" to unsubscribe all subscriptions?
Thanks for mentioning this point!
Yes it is necessary to unsubscribe Subject, BehaviorSubject subscriptions in ngOnDestroy() to avoid memory leaks & unexpected output.
Fantastic blog
Can you please post an article explaining how to use "TakeUntil" to unsubscribe all subscriptions?
Here you go mate :)
Fantastic blog !!! Short and informative
Thank u ...
Nice!!! its helpful
Thank u ...
Amazing one Diptee! Why don't you submit this to Google's Dev Library as well? devlibrary.withgoogle.com/
Thanks ankita.Sure will submit!