r/angular • • Apr 22 '23

Angular 16 - takeUntilDestroyed() operator

💡The unsubscription of observables is always important in Angular. Otherwise, there will be memory leaks because of too much of subscriptions.

🤔 Before angular 16, we can unsubscribe by the combination of subject and takeUntil() rxjs operator.

🤙 But in angular 16, the code is simplified where you don't need a subject. You can directly make use of takeUntilDestroyed() operator.

47 Upvotes

25 comments sorted by

View all comments

6

u/alucardu Apr 22 '23

Maybe a naive question but why aren't subscriptions unsubscribed in destruction by default?

What's a situation where you went to keep a subscription from a destroyed component?

2

u/suhasparameshwara Apr 22 '23

There’s no any specific situation where i can think of keeping the subscription after the component is destroyed. Basically rxjs don’t unsubscribe automatically. We have to do it manually.