Skip to content
πŸ€“ RxJS operator that unsubscribe from observables on destroy
Branch: master
Clone or download
Latest commit 459136a Jun 16, 2019

README.md

npm Build Status npm Awesome

πŸ€“ Angular - Unsubscribe For Pros πŸ’ͺ

Declarative way to unsubscribe from observables when the component destroyed

Not tested with Ivy

Installation

npm install ngx-take-until-destroy --save

Usage

import { untilDestroyed } from 'ngx-take-until-destroy';

@Component({
  selector: 'app-inbox',
  templateUrl: './inbox.component.html',
})
export class InboxComponent implements OnInit, OnDestroy {
  ngOnInit() {
    interval(1000)
      .pipe(untilDestroyed(this))
      .subscribe(val => console.log(val));
  }

  // This method must be present, even if empty.
  ngOnDestroy() {
    // To protect you, we'll throw an error if it doesn't exist.
  }
}

Use with any class

import { untilDestroyed } from 'ngx-take-until-destroy';

export class Widget {
  constructor() {
    interval(1000)
      .pipe(untilDestroyed(this, 'destroy'))
      .subscribe(console.log);
  }

  // The name needs to be the same as the second parameter
  destroy() {}
}

Live example

You can’t perform that action at this time.