angular - Angular 路由与某些功能模块的通用布局
问题描述
我有一个具有 3 个用户角色 A、B 和 C 的 Angular 7 应用程序。A 和 B 共享相同的视觉布局,但菜单项发生了变化(主题和结构相同,数据不同)。C具有完全不同的布局。
我的应用程序有AppModule
, SharedModule
, CoreModule
, HomeModule
, AModule
, BModule
& CModule
。我的常见布局位于一个名为的组件中,该组件CommonLayoutComponent
从中导出SharedModule
并用于模块 A 和 B。
工作流程:用户登陆具有三个按钮的主页,这些按钮将路由到模块 A、B 和 C。或者她可以通过为相关 URL 添加书签来直接导航到正确的模块。
我的 app-routing.module.ts
const routes: Routes = [
{ path: '', redirectTo: '/home', pathMatch: 'full' },
{ path: 'home', component: HomeComponent },
{ path: 'a', component: AComponent },
{ path: 'b', component: BComponent },
{ path: 'c', component: CComponent }
];
@NgModule({
imports: [RouterModule.forRoot(routes)],
exports: [RouterModule]
})
export class AppRoutingModule { }
问题:
AppComponent
即使我没有在这个组件中做任何事情,除了app.component.html 之外,我是否仍然应该在我的 AppModule<router-outlet></router-outlet>
中引导?- 如何将我的
CommonLayoutComponent
作为主/布局页面(如 ASP.NET MVC 布局页面)并将<router-outlet></router-outlet>
其弹出模块 A 和 B?
解决方案
由于您想为 A 和 B 保持相同的布局,因此将它们设为子路由并将布局相关的 html + 一个额外的路由器插座放入 CommonLayoutComponent 组件中。像这样的东西。。
const routes: Routes = [
{ path: '', redirectTo: '/home', pathMatch: 'full' },
{ path: 'home', component: HomeComponent },
{ path: '', component: CommonLayoutComponent, children:[
{ path: 'a', component: AComponent },
{ path: 'b', component: BComponent },
]},
{ path: 'c', component: CComponent }
];
推荐阅读
- asp.net-core - Blazor 组件 - 内联样式
- c++ - 从不同曝光时间的图像中获取 HDR 图像
- sql - 如何在 T-SQL 中识别和编辑匹配模式的所有实例
- amazon-s3 - AWS DMS Redshift 作为目标
- clojurescript - 正在使用哪些试剂 Form 2 组件函数参数?
- hyperledger-fabric - 如何在 hypledger fabric -ca 的 postgres-db 中启用 TLS?
- javascript - 无法更新 highcharts 图表中的绘图边框宽度
- kubernetes - 如何为 OpenShift 3.11 路由使用与默认通配符 dns 名称不同的 dns 名称?
- swift - 什么是 iOS < 9 的 CNLabeledValue 替代品?
- android - 如何处理 API URL 中的特殊字符 - 改造