angular - 具有延迟加载问题的角嵌套路由
问题描述
我有一个应用程序,我只需要在启动时就可以使用登录功能,并且所有剩余的代码都应该在用户身份验证后延迟加载。
我创建了一个带有core-routing.module和 core.component 的core.module来处理这个问题,但是子组件(例如 DashboardComponent)正在 app.component.html 上的 router-outlet 元素中呈现,而不是在core.component.html 等标题没有被显示。
我已经搜索了很多,但找不到如何让它工作。
应用程序路由.module.ts
const routes: Routes = [
{ path: '', redirectTo: 'signin', pathMatch: 'full' },
{ path: 'signin', component: SigninComponent },
{ path: 'app', loadChildren: './core/core.module#CoreModule', canLoad: [AuthGuard] },
{ path: '**', redirectTo: 'signin' }
];
@NgModule({
imports: [RouterModule.forRoot(routes)],
exports: [RouterModule]
})
export class AppRoutingModule {}
app.component.html
<router-outlet></router-outlet>
核心路由.module.ts
const routes: Routes = [
{ path: '', redirectTo: 'dashboard', pathMatch: 'full' },
{ path: 'dashboard', loadChildren: './dashboard/dashboard.module#DashboardModule', canLoad: [AuthGuard] },
{ path: '**', redirectTo: 'dashboard' }
];
@NgModule({
imports: [RouterModule.forChild(routes)],
exports: [RouterModule]
})
export class CoreRoutingModule { }
core.component.html
<div id="header">
<app-header></app-header>
</div>
<main id="content">
<router-outlet></router-outlet>
</main>
仪表板-routing.module.ts
const dashboardRoutes: Routes = [
{ path: '', component: DashboardComponent, pathMatch: 'full' }
];
@NgModule({
imports: [
CommonModule,
MaterialModule,
SharedModule,
RouterModule.forChild(dashboardRoutes)
],
解决方案
在尝试了许多不同的方式之后,对我有用的是将 core-routing.module.ts 更改为具有单个路由,并将延迟加载的模块作为子模块包含在其中:
const routes: Routes = [
{
path: '',
component: CoreComponent,
children: [
{ path: '', redirectTo: 'dashboard', pathMatch: 'full' },
{ path: 'dashboard', loadChildren: '../dashboard/dashboard.module#DashboardModule', canLoad: [AuthGuard] },
{ path: 'customers', loadChildren: '../customers/customers.module#CustomersModule', canLoad: [AuthGuard] },
{ path: 'history', loadChildren: '../history/history.module#HistoryModule', canLoad: [AuthGuard] },
{ path: 'processing', loadChildren: '../processing/processing.module#ProcessingModule', canLoad: [AuthGuard] },
{ path: '**', redirectTo: 'dashboard' }
]
}
];
希望这可以帮助尝试实现相同功能的人。
推荐阅读
- javascript - 如何返回字符串中具有特定值的对象?
- c++ - 大小为 K 的子集的积总和
- php - Laravel 雄辩查询的高级 where
- mongodb - Mongodb Atlas Connection String Does not work with Zapier
- java - 如何使用 biginteger 对两个边界之间的数字求和
- javascript - SweerAlert2 - 如何更改警报中的响应图像
- java - Java 中的自增运算符可以使用哪些类型的操作数?
- java - Hibernate - 选择 OneColumn 并以列表形式获取结果
- r - 如何从我的字符向量中删除前导制表符/空格?
- css - NPM 将多个目录中的多个 SCSS 文件合并到一个 css 文件中