How can I leverage ngOnDestroy at the application level and not at the component level?
I want to execute the following code when the user closes my application.
ngOnDestroy(): void {
localStorage.removeItem('token');
}
I put it into the AppComponent
like so:
export class AppComponent implements OnDestroy {
ngOnDestroy(): void {
localStorage.removeItem('token');
}
}
The OnDestroy
does not seem to fire when the application is closed. In this case I'm using the token in local storage to determine if the user has logged in or not by checking for the token and redirecting to the login page if it doesn't exists. This obviously doesn't work if the token isn't destroyed when the application is closed.
Where can I place this code to get the behavior I'm looking for, or is there a better way to accomplish what I'm trying to accomplish?