Tho*_*ris 8 angular-ui-router angular
有没有办法RouteReuseStrategy只针对特定路线实施?
这意味着每个路由都有子代,获得自己的自定义实现RouteReuseStrategy,并且只有在激活特定"树"中的路径时才触发其方法.
我目前使用这个答案中的代码,但是如果可能的话我想用上面的逻辑扩展它.
Plo*_*ppy 15
创建自定义路由重用策略
import { RouteReuseStrategy, ActivatedRouteSnapshot, DetachedRouteHandle } from "@angular/router";
export class CustomRouteReuseStategy implements RouteReuseStrategy {
handlers: { [key: string]: DetachedRouteHandle } = {};
shouldDetach(route: ActivatedRouteSnapshot): boolean {
return route.data.shouldReuse || false;
}
store(route: ActivatedRouteSnapshot, handle: {}): void {
if (route.data.shouldReuse) {
this.handlers[route.routeConfig.path] = handle;
}
}
shouldAttach(route: ActivatedRouteSnapshot): boolean {
return !!route.routeConfig && !!this.handlers[route.routeConfig.path];
}
retrieve(route: ActivatedRouteSnapshot): {} {
if (!route.routeConfig) return null;
return this.handlers[route.routeConfig.path];
}
shouldReuseRoute(future: ActivatedRouteSnapshot, curr: ActivatedRouteSnapshot): boolean {
return future.data.shouldReuse || false;
}
}
Run Code Online (Sandbox Code Playgroud)
在路由器模块中,在providers阵列中实施新策略:
providers: [
{ provide: RouteReuseStrategy, useClass: CustomRouteReuseStategy },
...
]
Run Code Online (Sandbox Code Playgroud)
然后,声明所需的路由,并将数据属性"shouldReuse"设置为true
{ path: 'myPath', component: MyComponent, data: { shouldReuse: true } },
Run Code Online (Sandbox Code Playgroud)
仅重用具有shouldReuse设置为data属性的路由true.