4

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:

  1. Programmatically change my route's shouldReuse value to false in the route config object.
  2. Navigate to some other route using this.router.navigate().
  3. Navigate to the route that I want to destroy.
  4. 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?

1
  • Hi, I have the exact same use case as you. Did you have any luck solving this?
    – kaiilim
    Feb 6, 2020 at 15:25

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Browse other questions tagged or ask your own question.