有条件地为angular2路由应用路由器重用策略

abh*_*ddy 8 angular

当我导航回相同的组件时,我开始了解Sticky Routes重新附加早期的组件数据.我已经通过查看此https://www.softwarearchitekt.at/post/2016/12/02/sticky-实现了一个演示 routes-in-angular-2-3-with-routereusestrategy.aspx 博客https://plnkr.co/edit/KVlRi9PtPeOpvn8bECBi?p=preview ...是否可以应用条件以便routerreusestrategy仅适用于少数组件?

Mic*_*cik 12

您只需对原始解决方案进行少量修改即可:https://www.softwarearchitekt.at/post/2016/12/02/sticky-routes-in-angular-2-3-with-routereusestrategy.aspx

添加shouldDetach标记到您的路线:

const appRoutes: Routes = [
  { path: 'crisis-center', component: CrisisListComponent, data: { shouldDetach: true } },
  ...
];
Run Code Online (Sandbox Code Playgroud)

并修改shouldDetach方法CustomReuseStrategy:

public shouldDetach(route: ActivatedRouteSnapshot): boolean {
  return route.data && (route.data as any).shouldDetach;
}
Run Code Online (Sandbox Code Playgroud)

这是您更新的plunker:https://plnkr.co/edit/otbZBuRmGYQXeY6b4Sfp p = preview