Angular 为什么Guard不';t在嵌套路线中工作-角度5
我已经创建了防护,以防止进入权限不足的页面 但我想知道,为什么我不得不在每条路线上增加警卫。我认为第一条路线上的警卫应该足够了,因为孩子们都是懒洋洋的。但事实并非如此。是我做错了什么,还是这就是Angular的工作原理 app.routing.ts 正如您在main.routing.ts中所看到的,我必须为所有路线添加防护,以使其正常工作。为什么呢Angular 为什么Guard不';t在嵌套路线中工作-角度5,angular,angular5,angular-routing,Angular,Angular5,Angular Routing,我已经创建了防护,以防止进入权限不足的页面 但我想知道,为什么我不得不在每条路线上增加警卫。我认为第一条路线上的警卫应该足够了,因为孩子们都是懒洋洋的。但事实并非如此。是我做错了什么,还是这就是Angular的工作原理 app.routing.ts 正如您在main.routing.ts中所看到的,我必须为所有路线添加防护,以使其正常工作。为什么呢 编辑: 我已经调试过,似乎没有进入AuthCanLoadGuard for MainModule。然而,对于bikeModule和CarsModu
编辑: 我已经调试过,似乎没有进入AuthCanLoadGuard for MainModule。然而,对于bikeModule和CarsModule来说,它们工作得很好。但我不知道为什么
编辑2: 问题解决了。问题在于路由配置
组
,因为父级已经有了组
,有什么问题试着激活孩子们。我不确定canLoad有什么用,从来没用过
编辑:但您的子路由看起来也有问题。他们不应该以“团体”为主角,因为这部分被父母抓住了。所以在main.routing
{path:“groups”中,
应该是{path:“,
etc没有帮助。顺便说一句,在使用canLoad时,模块不会在explorer中加载,因此对安全性有好处。这就是我使用它的原因。如果我从路径中删除组
,然后将我导航到带有组的屏幕。听起来你在Main中调用RouterModule.forRoot。应该是ForChild,并确保你没有将Main模块导入应用程序模块和一条评论,没有客户端安全。黑客可以得到你的JS代码,不管你做什么:)
export const routes: Routes = [
{ path: "", component: HomeComponent },
{ path: "groups", loadChildren: "app/main/main.module#MainModule",
canLoad: [AuthCanLoadGuard] } // <-- guard on first route
];
export const routes: Routes = [
{ path: "groups", component: GroupListComponent, canActivate: [AuthGuard] },
{ path: "groups/:id/category", component: CategoryComponent, canActivate: [AuthGuard] },
{
path: "groups/:id/category", component: GroupSummaryComponent, children: [
{ path: "bikes", canLoad: [AuthCanLoadGuard], loadChildren: "app/features/bikes/bikes.module#BikesModule" },
{ path: "cars", canLoad: [AuthCanLoadGuard] loadChildren: "app/features/cars/cars.module#CarsModule" },
]
}
];