angular - Angular 6延迟加载路由问题
问题描述
我想在你的应用程序中使用延迟加载。A 有 2 个模块:登录和汽车。延迟加载工作正常,但 route /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]
})
car.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]
})
解决方案
路径不能以斜杠开头。调整/add-car
为add-car
:
{
path: 'add-car',
component: AddCarComponent,
}
而且,如上面的答案所述,您还需要/cars
上下文。作为add-car
一个孩子,它仅在 下可用/cars
,因此您使用的路径在浏览器中是/cars/add-cars
推荐阅读
- mongodb - 对于模型“post”的路径“_id”中的值“comments”,转换为 ObjectId 失败
- python - 使用 pandas 计算大于先前值的值
- angular - Angular 7 新的路由代码.. 旧的 Angular 路由代码不起作用
- c# - 在集合中添加另一个对象时,如何覆盖 ObservableCollection 中的对象
- chef-infra - 厨师警告“没有找到配置文件:/solo.rb。改用命令行选项。”
- pandas - 熊猫将移动平均日基数转换为周基数
- sql - 在没有 DUAL 表的情况下使用 SYSDATE()
- numpy - 如何按列缩放特征?
- python - 如何解决此错误:预期 flatten_input 具有 3 个维度,但得到的数组形状为 (1, 28, 28, 3)?
- json - BSON 或 JSON crate 是否可以在找到一个无符号数时自动将字符串中的无符号数转换为浮点数?