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



我在应用程序路由模块中得到了这些路线:

{
  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"}两次。

孩子是否"采用"父母的数据?

您正在复制事物,儿童数组中的path : 'some'path: ''指向同一路线,仅将canActivate: [AuthGuard]放入孩子中,因此canActivate仅执行一次:

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

最新更新