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

时间:2017-01-05 10:55:40

标签: 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仅适用于少数组件吗?

1 个答案:

答案 0 :(得分: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 } },
  ...
];

并修改shouldDetach中的CustomReuseStrategy方法:

public shouldDetach(route: ActivatedRouteSnapshot): boolean {
  return route.data && (route.data as any).shouldDetach;
}

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