Javascript 路由器复用策略
当用户使用路由器重用策略关闭/刷新选项卡或(单击后退/前进)时,我试图持久化组件数据,但在添加CustomReuseStrategy提供程序routerLink后,它停止正常工作 登录后,我们被重定向到“/dashboard/begin”,如果我尝试通过routerLink转到“/dashboard/search”或“/dashboard/123”,则组件不会更改 但是,直接访问url的效果很好 有人经历过这种情况吗 app.module.tsJavascript 路由器复用策略,javascript,angular,Javascript,Angular,当用户使用路由器重用策略关闭/刷新选项卡或(单击后退/前进)时,我试图持久化组件数据,但在添加CustomReuseStrategy提供程序routerLink后,它停止正常工作 登录后,我们被重定向到“/dashboard/begin”,如果我尝试通过routerLink转到“/dashboard/search”或“/dashboard/123”,则组件不会更改 但是,直接访问url的效果很好 有人经历过这种情况吗 app.module.ts @NgModule({ providers:
@NgModule({
providers: [
AuthGuard,
{
provide: RouteReuseStrategy,
useClass: CustomReuseStrategy
}]
})
app.routing.ts
const ROUTES: Routes = [
{ path: 'dashboard', loadChildren: './dashboard/dashboard.module#DashboardModule', canActivateChild: [AuthGuard]},
{ path: 'login', component: LoginComponent },
{ path: '', redirectTo: '/login', pathMatch: 'full' }
]
const DASHBOARD_ROUTES: Routes = [
{
path: '', component: DashboardComponent,
children: [
{
path: 'begin',
component: BeginListComponent,
},
{
path: 'search',
children: [
{path: '', component: SearchListComponent },
{path: ':id', component: SearchIdComponent}
]
}
}
]
dashboard.routing.ts
const ROUTES: Routes = [
{ path: 'dashboard', loadChildren: './dashboard/dashboard.module#DashboardModule', canActivateChild: [AuthGuard]},
{ path: 'login', component: LoginComponent },
{ path: '', redirectTo: '/login', pathMatch: 'full' }
]
const DASHBOARD_ROUTES: Routes = [
{
path: '', component: DashboardComponent,
children: [
{
path: 'begin',
component: BeginListComponent,
},
{
path: 'search',
children: [
{path: '', component: SearchListComponent },
{path: ':id', component: SearchIdComponent}
]
}
}
]
custom.reuse.strategy.ts
从'@angular/router'导入{ActivatedRouteSnapshot,DetachedRouteHandle,RouteReuseStrategy};
导出类CustomReuseStrategy实现RouterUseStrategy{
公共静态处理程序:{[key:string]:DetachedRouteHandle}={};
私有静态waitDelete:string;
公共静态deleteRouteSnapshots():void{
CustomReuseStrategy.handlers={};
}
公共静态deleteRouteSnapshot(名称:字符串):void{
if(CustomReuseStrategy.handlers[名称]){
删除CustomReuseStrategy.handlers[name];
}否则{
CustomReuseStrategy.waitDelete=名称;
}
}
public shouldDetach(路由:ActivatedRouteSnapshot):布尔值{
控制台日志(路由);
如果(!路线){
CustomReuseStragey.deleteRouteSnapshots();
返回false;
}
if(route.params&&Object.keys(route.params).length>0){
返回false;
}
返回true;
}
公用存储(路由:ActivatedRouteSnapshot,句柄:DetachedRouteHandle):无效{
如果(
CustomReuseStrategy.waitDelete&&
CustomReuseStrategy.waitDelete==this.getRouteUrl(路由)
) {
CustomReuseStrategy.waitDelete=null;
返回;
}
CustomReuseStragey.handlers[this.getRouteUrl(route)]=句柄;
}
公共应附加(路由:ActivatedRouteSnapshot):布尔值{
return!!CustomReuseStrategy.handlers[this.getRouteUrl(route)];
}
公共检索(路由:ActivatedRouteSnapshot):DetachedRouteHandle{
如果(!route.routeConfig){
返回null;
}
返回CustomReuseStrategy.handlers[this.getRouteUrl(route)];
}
public shouldReuseRoute(future:ActivatedRouteSnapshot,curr:ActivatedRouteSnapshot):布尔值{
返回(
future.routeConfig==curr.routeConfig&&
JSON.stringify(future.params)==JSON.stringify(curr.params)
);
}
私有getRouteUrl(路由:ActivatedRouteSnapshot){
返回路由['''u routerState'].url.replace(//\//g,'''uz');
}
}
你找到答案了吗?你能澄清一下你最初的假设吗?您说过“当用户关闭/刷新选项卡或(单击后退/前进)时,我正在尝试持久化组件数据”。路由重用策略不是在用户关闭浏览器选项卡时记住页面的解决方案(除非使用与您使用的代码非常不同的代码,例如本地存储)。我猜我理解的不是你的意思。无论如何,在CustomReuseStragey方法中设置断点是否有助于找出RouterLink不起作用的原因?您是否找到了解决方法?您能否澄清您最初的假设?您说过“当用户关闭/刷新选项卡或(单击后退/前进)时,我正在尝试持久化组件数据”。路由重用策略不是在用户关闭浏览器选项卡时记住页面的解决方案(除非使用与您使用的代码非常不同的代码,例如本地存储)。我猜我理解的不是你的意思。无论如何,在CustomReuseStragey方法中设置断点是否有助于找出RouterLink不起作用的原因?