I have an Observable in my root (bootstrapped) Angular (6.x) component, AppComponent
.
Normally, I would unsubscribe any open Subscription upon a destroy() call using the lifecycle hook, ngOnDestroy
.
Since the AppComponent is the root of the application and therefore is never destroyed (except when the entire application is destroyed), do I still need to implement ngOnDestroy and do I need to bother with unsubscribing from my Subscriptions?
I haven't been able to find an answer to this, seemingly common, exact scenario.
Example:
export class AppComponent implements OnInit, OnDestroy {
private tokenSubscription: Subscription;
constructor(private dataSvc: DataService) { }
ngOnInit() {
this.tokenSubscription = this.dataSvc.myObservable.subscribe((val) => {
// do stuff
});
}
ngOnDestroy() {
this.tokenSubscription.unsubscribe(); // Do I need this in root component?
}
}
Thanks!