0

when a component is destroyed, does angular automatically "handles/closes/completes" the observables, and their related subscriptions, used by the component?

Or should I take care of them explicitly in the component's onDestroy?

According to the accepted answer to this question at least until April 2017 manual handling of observables is required.

I wonder if this is still the case

EDIT 1

On the angular docs I found this pipe which seems to solve the problem, but I'm not sure on how to use it in my components. Instead of setting a property in the subscribe.next, developer is supposed to store the observable in a property. How would the following component template become if I use the async pipe and set property to the observable?

<h1>{{property.title}}</h1>
<p>{{property.description</p>
Cec
  • 1,726
  • 3
  • 18
  • 31
  • 1
    The answer is still "it depends" – n00dl3 Feb 13 '18 at 09:15
  • 4
    Possible duplicate of [Angular/RxJs When should I unsubscribe from \`Subscription\`](https://stackoverflow.com/questions/38008334/angular-rxjs-when-should-i-unsubscribe-from-subscription) – n00dl3 Feb 13 '18 at 09:15

1 Answers1

1

You don`t need to unsubscribe from all observables.

You should unsubscribe from:

  • Form Controls
  • Custom observables, Basically or ones you create from subjects and such.
  • Third party observables in general

You do not need to unsubscribe from:

  • HttpCLient
  • Router
  • Some operators like take
  • Using async pipe

Extra resources:

https://www.reddit.com/r/Angular2/comments/66v9yy/so_we_should_never_really_unsubscribe_from/

Angular/RxJs When should I unsubscribe from `Subscription`

https://medium.com/@benlesh/rxjs-dont-unsubscribe-6753ed4fda87

Eduardo Vargas
  • 8,752
  • 2
  • 20
  • 27