I have an angular app where I am implementing a Route Reuse Strategy class which checks for a value stored on my route object in the shouldDetach()
method. Like this:
shouldDetach(route: ActivatedRouteSnapshot): boolean {
if (route.data.shouldReuse == true) {
return true;
}
else {
return false;
}
}
For the most part I want the component that is returned by the route to be cached when a user browses away... in other words I want any user customization of the state to persist and I want rxjs subscriptions to persist. But sometimes, on certain user input I want to be able to reset the route and allow for onDestroy to be fired on the route's component.
The only way I have found thus far to do this is to:
- Programmatically change my route's
shouldReuse
value tofalse
in the route config object. - Navigate to some other route using
this.router.navigate()
. - Navigate to the route that I want to destroy.
- Then navigate to some other route.
Only then does ngOnDestroy()
fire on the route I am trying to destroy... allowing me to unsubscribe from observables.
This is obviously a very hacky means of destroying my route's saved component. I need some other method of doing this in response to user action.
Is it possible to access a .destroy
method on a route directly? What about other methods in my Route Reuse Strategy class, can I leverage any of them to achieve this?