Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import {
- RouteReuseStrategy, DetachedRouteHandle, ActivatedRouteSnapshot, UrlSegment,
- ActivatedRoute, PRIMARY_OUTLET
- } from '@angular/router';
- export class CustomReuseStrategy implements RouteReuseStrategy {
- handlers: {[key: string]: DetachedRouteHandle} = {};
- shouldDetach(route: ActivatedRouteSnapshot): boolean {
- return !!this.getBreadcrumbs(route.root).join(".");
- }
- store(route: ActivatedRouteSnapshot, handle: DetachedRouteHandle): void {
- let url = this.getBreadcrumbs(route.root).join(".");
- this.handlers[url] = handle;
- }
- shouldAttach(route: ActivatedRouteSnapshot): boolean {
- let url = this.getBreadcrumbs(route.root).join(".");
- return !!this.handlers[url];
- }
- retrieve(route: ActivatedRouteSnapshot): DetachedRouteHandle {
- let url = this.getBreadcrumbs(route.root).join(".");
- if (!url) return null;
- return this.handlers[url];
- }
- shouldReuseRoute(future: ActivatedRouteSnapshot, curr: ActivatedRouteSnapshot): boolean {
- console.debug('CustomReuseStrategy:shouldReuseRoute', future, curr);
- return future.routeConfig === curr.routeConfig;
- }
- private getBreadcrumbs(route: ActivatedRouteSnapshot, url: string = "", breadcrumbs: string[] = []): string[] {
- //get the child routes
- let children: ActivatedRouteSnapshot[] = route.children;
- //return if there are no more children
- if (children.length === 0) {
- return breadcrumbs;
- }
- //iterate over each children
- for (let child of children) {
- //verify primary route
- if (child.outlet !== PRIMARY_OUTLET) {
- continue;
- }
- //get the route's URL segment
- let routeURL: string = child.url.map(segment => segment.path).join("/");
- //append route URL to URL
- url += `/${routeURL}`;
- breadcrumbs.push(url);
- //recursive
- return this.getBreadcrumbs(child, url, breadcrumbs);
- }
- return breadcrumbs;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement