Unsubscribe Observables in Angular

Unsubscribe Observables in Angular

ยท

1 min read

๐Ÿ’ก
This article is obsolete. Please follow the instructions at Angulars official documentary how to unsubscribe from observable in Angular way. https://angular.dev/api/core/rxjs-interop/takeUntilDestroyed#

I want to show you here how I personally unsubscribe observables that I manually subscribe without the async pipe.

export class DataTableExamplesComponent {

  private subs: Subscription[] = [];

  constructor(private myService: MyService) {
      const getDataSub = this.myService.getData$.subscribe((data) =>        console.log(data));
      this.subs.push(getDataSub);
  }

  public ngOnDestroy() {
    this.subs.forEach(s => s.unsubscribe());
  }

}
ย