15

Is there a way to implement RouteReuseStrategy only for specific routes?

Meaning each route with children, getting its own custom implementation of RouteReuseStrategy, and whose methods only fire when a route in a specific 'tree' is activated.

I currently use the code from this answer, but I want to expand it with the above logic if possible.

Thodoris
  • 712
  • 2
  • 8
  • 23

2 Answers2

35

Create a custom route reuse strategy

import { RouteReuseStrategy, ActivatedRouteSnapshot, DetachedRouteHandle } from "@angular/router";

export class CustomRouteReuseStrategy implements RouteReuseStrategy {

  handlers: { [key: string]: DetachedRouteHandle } = {};

  shouldDetach(route: ActivatedRouteSnapshot): boolean {
    return route.data.shouldReuse || false;
  }

  store(route: ActivatedRouteSnapshot, handle: {}): void {
    if (route.data.shouldReuse) {
      this.handlers[route.routeConfig.path] = handle;
    }
  }

  shouldAttach(route: ActivatedRouteSnapshot): boolean {
    return !!route.routeConfig && !!this.handlers[route.routeConfig.path];
  }

  retrieve(route: ActivatedRouteSnapshot): {} {
    if (!route.routeConfig) return null;
    return this.handlers[route.routeConfig.path];
  }

  shouldReuseRoute(future: ActivatedRouteSnapshot, curr: ActivatedRouteSnapshot): boolean {
    return future.data.shouldReuse || false;
  }

}

In your router module, implement the new strategy in the providers array:

providers: [
  { provide: RouteReuseStrategy, useClass: CustomRouteReuseStategy },
  ...
]

Then, declare the desired route with a data property 'shouldReuse' set to true

{ path: 'myPath', component: MyComponent, data: { shouldReuse: true } },

Only the routes with the data property shouldReuse set to true will be reused.

Maurice
  • 6,698
  • 9
  • 47
  • 104
Ploppy
  • 14,810
  • 6
  • 41
  • 58
  • 1
    pure gold! short and elegant... very nice – denns Dec 03 '20 at 08:46
  • When i implement this solution `firstChild` of `ActivatedRoute` becomes `null`. Any idea why? – Maurice Mar 18 '22 at 23:18
  • Nevermind, i've found out why. I need to set `data: { shouldReuse: true|false }` for _all_ the path definitions. Or else firstChild will become null for some reason. – Maurice Mar 19 '22 at 00:26
0

Yes, you can do this by writing your own RouteReuseStrategy (CustomReuseStrategy).

To black or white list routes, you can search for a data property which you can set in the router-module under the route and then choose to attach the component (to reuse it later), or not.

Helpful links to get you started:

Jmsdb
  • 515
  • 3
  • 9
  • 3
    This is exactly what your are not supposed to do on SO, link to external websites because those websites can go down at any time. – Ploppy Jun 09 '19 at 07:45
  • 1
    Thanks for the information, I'll actually go read the guidelines now, makes total sense. This was a very common issue, with many explainations far better then my own - and felt sharing these, would help best. Edit: after reading the guidelines, linking external is fine, but the most import information is referenced and given on SO (for any future readers) – Jmsdb Jun 10 '19 at 08:43