gpt4 book ai didi

带有路由的 Angular 6 延迟加载问题

转载 作者:搜寻专家 更新时间:2023-10-30 21:26:42 26 4
gpt4 key购买 nike

我想在您的应用中使用延迟加载。 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]
})

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]
})

最佳答案

路径不能以斜杠开头。调整/add-caradd-car:

{
path: 'add-car',
component: AddCarComponent,

}

并且,如上面的答案所述,您还需要 /cars 上下文。由于 add-car 是一个 child ,它只能在 /cars 下使用,所以您在浏览器中使用的路径是 /cars/add-cars

关于带有路由的 Angular 6 延迟加载问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53874975/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com