I implemented RouteReuseStrategy
suggested here and also updated a bit, because on shouldAttach
the routeConfig.path
was empty, and the handler was not cached. I have @angular/router: 3.4.7
.
import {RouteReuseStrategy, DetachedRouteHandle, ActivatedRouteSnapshot} from "@angular/router"
export class CustomReuseStrategy implements RouteReuseStrategy {
handlers: {[key: string]: DetachedRouteHandle} = {};
currentPath: string = '';
shouldDetach(route: ActivatedRouteSnapshot): boolean {
return true
}
store(route: ActivatedRouteSnapshot, handle: DetachedRouteHandle): void {
this.handlers[route.routeConfig.path] = handle
}
shouldAttach(route: ActivatedRouteSnapshot): boolean {
// todo route.routeConfig.path was empty
if (!!this.currentPath && !!this.handlers[this.currentPath]) {
route.routeConfig.path = this.currentPath;
return true
} else {
return false
}
}
retrieve(route: ActivatedRouteSnapshot): DetachedRouteHandle {
if (!route.routeConfig) return null;
return this.handlers[route.routeConfig.path]
}
shouldReuseRoute(future: ActivatedRouteSnapshot, curr: ActivatedRouteSnapshot): boolean {
this.currentPath = curr.routeConfig && curr.routeConfig.path;
return future.routeConfig === curr.routeConfig
}
deleteHandler(handler:string) {
this.handlers[handler] && delete this.handlers[handler]
}
}
Everything is working, except when I try to navigate back the 3rd time to a route I got the following error:
EXCEPTION: Uncaught (in promise): Error: Cannot reattach ActivatedRouteSnapshot with a different number of children
Error: Cannot reattach ActivatedRouteSnapshot with a different number of children
at setFutureSnapshotsOfActivatedRoutes (http://localhost:4200/js/vendor.bundle.js:93110:15) [angular]
at createNode (http://localhost:4200/js/vendor.bundle.js:93091:9) [angular]
at http://localhost:4200/js/vendor.bundle.js:93131:16 [angular]
at Array.map (native) [angular]
at createOrReuseChildren (http://localhost:4200/js/vendor.bundle.js:93124:26) [angular]
at createNode (http://localhost:4200/js/vendor.bundle.js:93086:41) [angular]
at http://localhost:4200/js/vendor.bundle.js:93128:24 [angular]
at Array.map (native) [angular]
at createOrReuseChildren (http://localhost:4200/js/vendor.bundle.js:93124:26) [angular]
at createNode (http://localhost:4200/js/vendor.bundle.js:93086:41) [angular]
at createRouterState (http://localhost:4200/js/vendor.bundle.js:93072:33) [angular]
at MapSubscriber.project (http://localhost:4200/js/vendor.bundle.js:26366:153) [angular]
at MapSubscriber._next (http://localhost:4200/js/vendor.bundle.js:15890:35) [angular]
at MapSubscriber.Subscriber.next (http://localhost:4200/js/vendor.bundle.js:4861:18) [angular]
at MergeMapSubscriber.notifyNext (http://localhost:4200/js/vendor.bundle.js:19133:30) [angular]
at InnerSubscriber._next (http://localhost:4200/js/vendor.bundle.js:106234:21) [angular]
at InnerSubscriber.Subscriber.next (http://localhost:4200/js/vendor.bundle.js:4861:18) [angular]
at MapSubscriber._next (http://localhost:4200/js/vendor.bundle.js:15896:26) [angular]
at MapSubscriber.Subscriber.next (http://localhost:4200/js/vendor.bundle.js:4861:18) [angular]
at ReduceSubscriber._complete (http://localhost:4200/js/vendor.bundle.js:36808:30) [angular]
at ReduceSubscriber.Subscriber.complete (http://localhost:4200/js/vendor.bundle.js:4886:18) [angular]
at MergeMapSubscriber._complete (http://localhost:4200/js/vendor.bundle.js:19125:30) [angular]
at MergeMapSubscriber.Subscriber.complete (http://localhost:4200/js/vendor.bundle.js:4886:18) [angular]
at ArrayObservable._subscribe (http://localhost:4200/js/vendor.bundle.js:12465:24) [angular]
at ArrayObservable.Observable._trySubscribe (http://localhost:4200/js/vendor.bundle.js:221:25) [angular]
at ArrayObservable.Observable.subscribe (http://localhost:4200/js/vendor.bundle.js:209:27) [angular]
at MergeMapOperator.call (http://localhost:4200/js/vendor.bundle.js:19075:23) [angular]
at Observable.subscribe (http://localhost:4200/js/vendor.bundle.js:206:22) [angular]
at ReduceOperator.call (http://localhost:4200/js/vendor.bundle.js:36763:23) [angular]
at Observable.subscribe (http://localhost:4200/js/vendor.bundle.js:206:22) [angular]
at MapOperator.call (http://localhost:4200/js/vendor.bundle.js:15867:23) [angular]
at Observable.subscribe (http://localhost:4200/js/vendor.bundle.js:206:22) [angular]
at Object.subscribeToResult (http://localhost:4200/js/vendor.bundle.js:5096:27) [angular]
...