路由上的Angular2路由器数据属性

时间:2017-03-11 18:08:12

标签: angular router

我在app-routing模块中获得了这些路由:

{
  path: 'some',
  canActivate: [AuthGuard],
  data: {
    auth: {
      test: "theTest"
    },
  },
  children: [
    {
      path: '',
      pathMatch: 'full',
      component: SomeComponent,
      canActivate: [AuthGuard],
      resolve: {
        something: SomethingResolver
      }
    },
  ]
}

在AuthGuard CanActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot)中,如果我执行console.log(route.data["auth"]并获得{test: "theTest"}两次。

孩子们采取"采取"父母的数据?

1 个答案:

答案 0 :(得分:0)

你是重复的东西,children数组中的path : 'some'path: ''指向同一路径,只将canActivate: [AuthGuard]放在子节点中,以便执行canActivate只有一次:

{
  path: 'some',
  data: {
      auth: {
         test: "theTest"
      }
   },
  children: [
    {
      path: '',
      pathMatch: 'full',
      component: SomeComponent,
      canActivate: [AuthGuard],
      resolve: {
        something: SomethingResolver
      }
    },
  ]
}