I am looking to conditionally run some code in the ngOnDestroy
function depending on changes in the current route.
Specifically, when the route changes from /foo
to /login?logout=true
, and this change is initiated outside of the Foo component.
In the ngOnInit
method, I am subscribing to query parameter changes to correctly update the loggingOut
flag.
The issue I'm facing is that ngOnDestroy
is being called before the query parameter handler, resulting in the loggingOut
flag having the wrong value.
export class FooComponent implements OnInit, OnDestroy {
loggingOut = false;
constructor(private route: ActivatedRoute) {}
ngOnInit(): void {
this.route.queryParamMap.subscribe(queryParams => {
this.loggingOut = queryParams.get('logout') === 'true';
});
}
ngOnDestroy(): void {
if (this.loggingOut) {
// do this
} else {
// do that
}
}
}
It appears this behavior is intentional from a lifecycle perspective, so my question is:
- Is there a way to check route changes before
ngOnDestory
is called?
If possible, please provide a link to documentation that explains how lifecycle hooks, especially ngOnDestory
, are triggered in relation to navigation changes.
Thank you.