To expand on @AaronSterling’s comment, I never explicitly type unsubscribe
any more, after reading this article. Instead, I use the following idiom:
private _tripwire: Subject<boolean>;
ionViewWillEnter(): void {
this._tripwire = new Subject<boolean>();
observableFoo()
.pipe(takeUntil(this._tripwire))
.subscribe(foo => this.foo = foo);
observableBar()
.pipe(takeUntil(this._tripwire))
.subscribe(bar => this.bar = bar);
// repeat ad infinitum
}
ionViewWillLeave(): void {
// it would be nice to be able to just complete, but that won't trigger takeUntil
this._tripwire.next(true);
// strictly speaking probably not necessary, but i do it in case the previous line gets to go away at some point
this._tripwire.complete();
this._tripwire = undefined;
}
Instead of willEnter/willLeave, these can be paired up in didLoad/willUnload or any other set of matching lifecycle events as befits your situation.