8

I came to know about Sticky Routes to reattach the earlier component data when navigated back to the same component.I have implemented a demo by looking at this https://www.softwarearchitekt.at/post/2016/12/02/sticky-routes-in-angular-2-3-with-routereusestrategy.aspx blog here https://plnkr.co/edit/KVlRi9PtPeOpvn8bECBi?p=preview ... Is it Possible to have the apply conditions so that routerreusestrategy apply only for few components?

abhilash reddy
  • 1,546
  • 8
  • 31
  • 53
  • Possible duplicate of [How to implement RouteReuseStrategy shouldDetach for specific routes in Angular 2](http://stackoverflow.com/questions/41280471/how-to-implement-routereusestrategy-shoulddetach-for-specific-routes-in-angular) – Dale Harris Mar 01 '17 at 17:08

1 Answers1

12

You need only minor modification of the original solution: https://www.softwarearchitekt.at/post/2016/12/02/sticky-routes-in-angular-2-3-with-routereusestrategy.aspx

Add shouldDetach flag to your route:

const appRoutes: Routes = [
  { path: 'crisis-center', component: CrisisListComponent, data: { shouldDetach: true } },
  ...
];

And modify shouldDetach method in CustomReuseStrategy:

public shouldDetach(route: ActivatedRouteSnapshot): boolean {
  return route.data && (route.data as any).shouldDetach;
}

Here is your plunker updated: https://plnkr.co/edit/otbZBuRmGYQXeY6b4Sfp?p=preview

Michal Moravcik
  • 2,250
  • 20
  • 18