ngx-возьми-пока-уничтожь - это элегантный способ достичь этого!
https://www.npmjs.com/package/ngx-take-until-destroy
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.
}
}