Angular Angular 中的粘性状态 2+

Angular Sticky States in Angular 2+

我正在将 Angular 1.5.X 应用迁移到 Angular 4。 在我的应用程序中,我使用 Angular Ui-Router Sticky State https://github.com/ui-router/sticky-states to avoid lose the content of my view when I made a navigation in some views of my APP but when I try to use this functionality in ng-router2 I can't find it. https://ui-router.github.io/ng2/tutorial/helloworld.

有些人知道一些路由器在 Angular2+ 中具有与 Sticky State 类似的功能,或者使用 ui-router-ng2 来模拟 Sticky State。

我在互联网上找到了这个答案我不记得是在网上,但解决方案不是我的。

import { Injectable } from '@angular/core';

import { Router, CanActivate, ActivatedRouteSnapshot, RouterStateSnapshot, RouteReuseStrategy, DetachedRouteHandle, Route} from '@angular/router';

@Injectable()
export class ArchReuseStrategyService implements RouteReuseStrategy {

    handlers: {[key: string]: DetachedRouteHandle} = {};

     /** Determine if should detach the route */
    shouldDetach(route: ActivatedRouteSnapshot): boolean {
        console.log('CustomReuseStrategy:shouldDetach', route);
        return (route.routeConfig.sticky === true) ? true : false;
        // Or return true; 
    }
    /** Store the route */
    store(route: ActivatedRouteSnapshot, handle: DetachedRouteHandle): void {
        console.log('CustomReuseStrategy:store', route, handle);
        this.handlers[route.routeConfig.path] = handle;
    }
    /** Determina si la ruta y su subrutas deberian poder ser recuperadas */
    shouldAttach(route: ActivatedRouteSnapshot): boolean {
        console.log('CustomReuseStrategy:shouldAttach', route);
        return !!route.routeConfig && !!this.handlers[route.routeConfig.path];
    }
    /** Recover a store route*/
    retrieve(route: ActivatedRouteSnapshot): DetachedRouteHandle {
        console.log('CustomReuseStrategy:retrieve', route);
        if (!route.routeConfig) return null;
        return this.handlers[route.routeConfig.path];
    }
    /** Determine if the route should be reused*/
    shouldReuseRoute(future: ActivatedRouteSnapshot, curr: ActivatedRouteSnapshot): boolean {
        console.log('CustomReuseStrategy:shouldReuseRoute', future, curr);
        return future.routeConfig === curr.routeConfig;
    }
}