带有功能的AOT路由突变会增加防护吗?

时间:2019-06-25 09:42:15

标签: angular angular2-routing

因此:我有一个“问题”,其中我有太多的路由,需要为所有这些路由添加防护。由于我使用的是“带壳路线”,因此我想在给它们加壳时会对其进行突变,因此,这里的问题是“突变路线”作为空路线配置“ Invalid configuration of Route ''.”加载,...我可以在AOT中这样做吗?还是我坚持在所有路线上都亲自写警戒线?

export class Route {
  static withShell(routes: Routes): Routes {
    return [{
      path: '',
      component: ShellComponent,
      children: [
        {
          path: '',
          canActivate: [AuthenticationGuard],
          children: ParseRoutesForUrlbarChangeGuardIntroduction(routes)
        }
      ],
      canActivate: [SingleSignOnGuard, RedirectGuard]
    }];
  }
}


export function RouteWithPreventUrlGuard(route: Route) {
  if (route.redirectTo || !route.component) return route;
  if (route.children) {
    route.children.forEach((r, i) => {
      const canDeactivate = r.canDeactivate;
      if (!canDeactivate || canDeactivate && !canDeactivate.find(g => g instanceof PreventUrlbarChangeGuard))
        route.children[i] = RouteWithPreventUrlGuard(r);
    });
  }
  return {...route, canDeactivate: [PreventUrlbarChangeGuard, ...(route.canDeactivate || [])]};
}

export function ParseRoutesForUrlbarChangeGuardIntroduction(routes: Route[]) {
  return routes.map(r => RouteWithPreventUrlGuard(r));
}

0 个答案:

没有答案