RxJS 6 examples in Angular 6 – Unsubscribe from Observables

RxJS is great but it is important you unsubscribe from observables to avoid memory leaks.


Unsubscribe from observables

The pattern I use for unsubscribing from observables is takeUntil(). It looks like this:

    .subscribe(value => this.myData = value);

This tells myObservable to keep taking values until this.unsubscribeAll emits a value.

Here’s the full example on StackBlitz.


async pipe

There is an easier way to subscribe to an observable in Angular. The async pipe automatically unpacks your data and unsubscribes from the observable when your component is removed from the DOM.


Other articles in this series


Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.