🤓 Angular - Unsubscribe For Pros 💪
Declarative way to unsubscribe from observables when the component destroyed
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));
}
ngOnDestroy() {
}
}
Use with any class
import { untilDestroyed } from 'ngx-take-until-destroy';
export class Widget {
constructor() {
interval(1000)
.pipe(untilDestroyed(this, 'destroy'))
.subscribe(console.log);
}
destroy() {}
}
Live example