我正在将Angular 1.5.X应用迁移到Angular 4。 在我的应用程序中,我使用Angular Ui-Router Sticky State https://github.com/ui-router/sticky-states以避免在我的APP的某些视图中导航时丢失我的视图内容但是当我尝试在ng-router2中使用此功能时我可以&# 39;找不到它。 https://ui-router.github.io/ng2/tutorial/helloworld
有人知道一些路由器在Angular2 +中具有与Sticky State类似的功能,或者使用ui-router-ng2来模拟Sticky State。
答案 0 :(得分:1)
我在互联网上找到了这个答案我不记得网络,但解决方案不是我的。
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;
}
}