Automatically Unsubscribing Angular Observables

Whenever you need to automatically unsubscribe observable to avoid memory leaks, you can use @ngneat/until-destroy package.

You can add this package by following easy steps below.

Package

@ngneat/until-destroy

A neat way to unsubscribe from observables when the component destroyed

Install

npm i @ngneat/until-destroy

Import

import { untilDestroyed, UntilDestroy } from '@ngneat/until-destroy';

Use


@UntilDestroy()
@Component({
  ...............
})

And

 this.service.functionName().pipe(untilDestroyed(this))

      .subscribe(data => {

        ......

      });

 

That's It. Enjoying your unsubscribing :)

Leave a comment

your email address will not be published. required fields are marked *

Name *
Email *
Phone