angularobservableangular5angular-component-life-cycle

Taking care of observables and subscriptions upon components' onDestroy


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>

Solution

  • You don`t need to unsubscribe from all observables.

    You should unsubscribe from:

    You do not need to unsubscribe from:

    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