我最近构建了这个,我所做的只是将路线分成两个不同的模块。
所以你会有这个:
- +admin
- routes
- +dashboard
- +login
- ... etc
- +website
- routes
- +home
- +profile
- ... etc
那么你想要做的是使用canLoad 保护来防止在你没有被授权的情况下加载模块。这将保护前端的管理区域,以便除非您是具有该权限的管理员,否则代码不会暴露。
如果您不想将项目拆分为两个较小的项目,这是最简单的方法。我个人不会这样做,因为跨应用程序共享内容变得更加复杂。
编辑:
路由看起来像这样:
const routes: Routes = [
{
path: '',
loadChildren: 'app/+website/website.module#WebsiteModule'
},
{
path: 'admin',
loadChildren: 'app/+admin-area/admin-area.module#AdminAreaModule'
}
];
@NgModule({
imports: [
RouterModule.forRoot(routes)
],
exports: [
RouterModule,
AppComponent
],
declarations: [
AppComponent
]
})
export class AppRouterModule {}
所以只需转到/admin 就会加载管理区域模块,该模块将有另一个路由器模块看起来像这样:
const routes: Routes = [
{
path: '',
component: AdminAreaComponent,
children: [
{
path: 'login',
loadChildren: 'app/+admin-area/pages/+login/login.module#LoginModule'
},
{
path: 'dashboard',
loadChildren: 'app/+admin-area/pages/+dashboard/dashboard.module#DashboardModule',
canLoad: [AuthGuardService]
}
]
}
];
@NgModule({
imports: [
ComponentsModule,
SharedModule,
RouterModule.forChild(routes)
],
declarations: [
AdminAreaComponent
]
})
export class AdminAreaRouterModule {}
在这里您可以看到/admin/dashboard 受到检查用户角色的守卫的保护。