Custom RouteReuseStrategy for Angular's child module
Asked Answered
C

3

8

I want to use this custom route reuse strategy for just one module:

export class CustomRouteReuseStrategy extends RouteReuseStrategy {
    public shouldDetach(route: ActivatedRouteSnapshot): boolean { return false; }
    public store(route: ActivatedRouteSnapshot, detachedTree: DetachedRouteHandle): void {}
    public shouldAttach(route: ActivatedRouteSnapshot): boolean { return false; }
    public retrieve(route: ActivatedRouteSnapshot): DetachedRouteHandle { return null; }
    public shouldReuseRoute(future: ActivatedRouteSnapshot, curr: ActivatedRouteSnapshot): boolean {
        return true;
    }
}

So I've passed into @NgModule() in one of my modules named ChildModule:

providers: [
        {
            provide: RouteReuseStrategy,
            useClass: CustomRouteReuseStrategy
        }
]

Unfortunately, when I pass it there it simply gets ignored. Although works fine when added to my root AppModule... I'm not sure if it matters, but ChildModule is lazily loaded. How to solve it?

Connected answered 2/7, 2017 at 22:10 Comment(2)
I think RouteReuseStrategy should be provide in the root level. It wont work in module levelTerritorialize
In my case it didn't work because some people manually overrode it in some components. Those class methods are not constant.Luncheon
C
21

I finally achieved it by passing a bit modified CustomRouteStrategy to AppModule:

export class CustomRouteReuseStrategy extends RouteReuseStrategy {
    public shouldDetach(route: ActivatedRouteSnapshot): boolean { return false; }
    public store(route: ActivatedRouteSnapshot, detachedTree: DetachedRouteHandle): void {}
    public shouldAttach(route: ActivatedRouteSnapshot): boolean { return false; }
    public retrieve(route: ActivatedRouteSnapshot): DetachedRouteHandle { return null; }
    public shouldReuseRoute(future: ActivatedRouteSnapshot, curr: ActivatedRouteSnapshot): boolean {
        return (future.routeConfig === curr.routeConfig) || future.data.reuse;
    }
}

And adding data: { reuse: true } to the routing of lazily loaded ChildModule:

{
    path: 'some-path',
    data: { reuse: true },
    loadChildren: './child.module#ChildModule',
},

Demo with more advanced solution

Connected answered 3/7, 2017 at 0:42 Comment(3)
how can we check current and comeback have same instance?Duly
you can set a breakpoint into the constructor / oninit and see if its only called once.Brigitta
now i have 2 dynamic tabs on screen one is emp other one is dept , now i will open emp tab and i will type something in emp tab next i will switch from emp tab to dept tab and i will come back to emp ... now what happens to previously entered text ?Muna
W
5

CUSTOM ROUTE STRATEGY

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

export class CustomReuseStrategy implements RouteReuseStrategy {

  public static handlers: { [key: string]: DetachedRouteHandle } = {}

  private static delete: string

  //THIS METHOD IS USED FOR DELETE ROUTE
  public static deleteRouteSnapshot(name: string): void {
      if (CustomReuseStrategy.handlers[name]) {
          delete CustomReuseStrategy.handlers[name];
      } else {
          CustomReuseStrategy.delete = name;
      }
  }

  //THIS METHOD RETURN TRUE WHEN ROUTE REUSE LATER
  public shouldDetach(route: ActivatedRouteSnapshot): boolean {
      return true;
  }

  //THIS METHOD IS USD FOR STORE ROUTE STATE
  public store(route: ActivatedRouteSnapshot, handle: DetachedRouteHandle): void {
      if (CustomReuseStrategy.delete && CustomReuseStrategy.delete == this.getRouteUrl(route)) {
          CustomReuseStrategy.delete = null
          return;
      }
      CustomReuseStrategy.handlers[this.getRouteUrl(route)] = handle;
  }

  //ATTACHED ROUTE IF ALREADY NOT PRESENT
  public shouldAttach(route: ActivatedRouteSnapshot): boolean {
      return !!CustomReuseStrategy.handlers[this.getRouteUrl(route)];
  }

  //THIS METHOD IS USED FOR RETRIEVING ROUTE STATE
  public retrieve(route: ActivatedRouteSnapshot): DetachedRouteHandle {
      if (!route.routeConfig) {
          return null
      }
      return CustomReuseStrategy.handlers[this.getRouteUrl(route)];
  }

  //THIS METHOD RUN WHEN USER CHANGE ROUTE EVEY TIME AND CHECK CURRENT ROUTE WANT TO USED CUSTOM STRATEGY OR NOT
  public shouldReuseRoute(future: ActivatedRouteSnapshot, curr: ActivatedRouteSnapshot): boolean {
      return future.routeConfig === curr.routeConfig &&
          JSON.stringify(future.params) === JSON.stringify(curr.params);
  }

  //FIND OUT ACTUAL ROUTE NAME AND ROUTE THE URL
  private getRouteUrl(route: ActivatedRouteSnapshot) {
      return route['_routerState'].url.replace(/\//g, '_')
  }
}
Wesleyanism answered 17/5, 2018 at 7:42 Comment(1)
you shouldn't use the private member of route: _routerStateQuestionless
S
0

Here is my working example for routes with children and parameters:

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

export class CustomReuseStrategy 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.getUrl(route)) {
        this.handlers[this.getUrl(route)] = handle;
    }
}

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

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

shouldReuseRoute(future: ActivatedRouteSnapshot, curr: ActivatedRouteSnapshot): boolean {
    return future.routeConfig === curr.routeConfig && JSON.stringify(future.params) === JSON.stringify(curr.params);
}

getUrl(route: ActivatedRouteSnapshot) {
    if (!route.parent.url.join('/') || !route.url.join('/')) {
        return null;
    }
    let url = '';
    if (route.parent.url.join('/')) {
        url += route.parent.url.join('/') + '/';
    }
    if (route.url.join('/')) {
        url += route.url.join('/');
    }
    return url === '' ? null : url;
}
}

And inside routing config:

export const myRoute: Route = {

    path: 'my',
    component: MyComponent,
    data: {
        pageTitle: 'MY'
    },
    children: [
        {
            path: '',
            redirectTo: 'dashboard',
            pathMatch: 'full'
        },
        {
            path: 'dashboard',
            component: MyDashboardComponent,
            data: {
                shouldReuse: true
            }
        },
        {
            path: 'orders',
            component: MyOrdersComponent,
            data: {
                shouldReuse: true
            }
        },
        {
            path: 'event/:id',
            component: MyEventComponent,
            data: {
                shouldReuse: true
            }
        }
    ]
};
Sphere answered 21/11, 2018 at 11:23 Comment(1)
There might be a mistake in the getUrl method: I am pretty sure it should be if (!route[...] && !route[...]) with an && in the return null section. Otherwise the ifs in the part below would be useless.Merge

© 2022 - 2024 — McMap. All rights reserved.