Declarative way to unsubscribe from observables when the component destroyed
$ npm install @orchestrator/ngx-until-destroyed --save
import { untilDestroyed } from '@orchestrator/ngx-until-destroyed';
@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.
}
}
import { WithUntilDestroyed } from '@orchestrator/ngx-until-destroyed';
@Component({...})
class MyComponent implements OnDestroy {
@WithUntilDestroyed()
stream$ = interval(1000); // You can safely subscribe to this everywhere
// This method must be present, even if empty
ngOnDestroy() {}
}
import { untilDestroyed } from '@orchestrator/ngx-until-destroyed';
export class Widget {
constructor() {
interval(1000)
.pipe(untilDestroyed(this, 'destroy'))
.subscribe(console.log);
}
// The name needs to be the same as the decorator parameter
destroy() {}
}