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?
Custom Route Reuse StrategyshouldDetach : Determines if this route (and its subtree) should be detached to be reused later. store : Stores the detached route. shouldAttach : Determines if this route (and its subtree) should be reattached. retrieve : Retrieves the previously stored route.
BaseRouteReuseStrategylink This base route reuse strategy only reuses routes when the matched router configs are identical. This prevents components from being destroyed and recreated when just the fragment or query parameters change (that is, the existing component is reused).
RunGuardsAndResolverslink type-alias. A policy for when to run guards and resolvers on a route.
shouldReuseRoute This method is called everytime we navigate between routes. The future is the route we are leaving (not sure why is called future) and curr is the route we are landing. If it returns TRUE the routing will not happen (which means that routing has not changed).
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With