我想在您的应用中使用延迟加载。一个有2个模块:登录和汽车。延迟加载工作正常,但路由/ add-car不起作用。为什么? 找不到/ add-car路径,并重定向到PageNotFoundComponent。
app.routing.module.ts
const appRoutes: Routes = [
{
path: '',
pathMatch: 'full',
redirectTo: 'login'
},
{
path: 'cars',
canLoad: [AuthCanLoadGuard],
loadChildren: './cars/cars.module#CarsModule',
},
{
path: 'user-account',
component: UserAccountComponent,
canActivate: [AuthGuardsService]
},
{
path: '**',
component: PageNotFoundComponent
}
];
@NgModule({
imports: [RouterModule.forRoot(appRoutes, {enableTracing: true})],
exports: [RouterModule]
})
cars.routing.module.ts
const carsRoutes: Routes = [
{
path: '',
component: CarsComponent,
children: [
{
path: '',
component: CarsListComponent,
resolve: { cars : CarsListResolve } // przeniesione z app.routing.module.ts
},
{
path: ':id',
component: CarsDetailsComponent,
canDeactivate: [FormCanDeactivateGuard],
resolve: { car: CarResolve }
}
]
},
{
path: '/add-car',
component: AddCarComponent,
}
];
@NgModule({
imports: [RouterModule.forChild(carsRoutes)],
exports: [RouterModule]
})
答案 0 :(得分:2)
路径不能以斜杠开头。将/add-car
调整为add-car
:
{
path: 'add-car',
component: AddCarComponent,
}
并且,如上面的答案中所述,您还需要/cars
上下文。由于add-car
是孩子,因此仅在/cars
下可用,因此您在浏览器中使用的路径为/cars/add-cars
答案 1 :(得分:1)
您应该重定向到cars/add-car
,因为add-car是cars.routing.module.ts
的一部分;另外,在路由时,请确保使用{ relativeTo: this.route }
。
例如:this.router.navigate(['../add-car'], { relativeTo: this.route });
答案 2 :(得分:0)
如果我可以获得/ car-add路径,我应该在app.routing.module.ts中添加此路径吗? 在汽车模块之外添加addCarComponent是正确的吗?