我正在寻找一个Angular 2的解决方案,用于下面解释的场景:
在这种情况下,top-nav包含加载子模块的链接,sub-nav有链接来更新子模块的内容.
网址应映射为:
应用程序模块(和应用程序组件)包含一个导航到不同子模块的顶部导航栏,应用程序组件模板可能如下所示
<top-navbar></top-navbar>
<router-outlet></router-outlet>
Run Code Online (Sandbox Code Playgroud)
但这是复杂性.我需要我的子模块与第二级导航栏和他们自己的路由器插座具有相似的布局,以加载他们自己的组件.
<sub-navbar></sub-navbar>
<router-outlet name='sub'></router-outlet>
Run Code Online (Sandbox Code Playgroud)
我尝试了所有选项并在任何地方搜索但是找不到具有路由器插座的子模块中的默认模板(如app组件)的解决方案,并且还在内部路由器插座中加载子模块的内容而不会丢失子导航.
我会很感激任何意见或想法
Asw*_* KV 43
html页面看起来像这样.
主页
<top-navbar></top-navbar>
<router-outlet></router-outlet>
Run Code Online (Sandbox Code Playgroud)
子模块页面
<sub-navbar></sub-navbar>
<router-outlet name='sub'></router-outlet>
Run Code Online (Sandbox Code Playgroud)
点击顶部导航栏中的导航,主要路径出口将分别路由.
点击子导航栏时,router-outlet [sub]将分别路由.
HTML很好,诀窍就在于编写app.routing
app.routing.ts
const appRoutes: Routes = [
{
path: 'login',
component: LoginComponent
},
{ path: 'home',
component: homeComponent,
children: [
{
path: 'module1',
component: module1Component,
children: [
{
path: 'submodule11',
component: submodule11Component,
},
{
path: '',
redirectTo: 'submodule11',
pathMatch: 'full'
}
]
},
{
path: 'module2',
component: module2omponent,
children: [
{
path: 'submodule21',
component: submodule21Component,
},
{
path: '',
redirectTo: 'submodule21',
pathMatch: 'full'
}
]
}
]
},
{
path: 'about',
component: aboutComponent
}
]
Run Code Online (Sandbox Code Playgroud)
希望它会对你有所帮助.
更多细节https://angular.io/guide/router
小智 16
使用:
RouterModule.forChild()
...
<router-outlet name="sub"></router-outlet>
...
[routerLink]="[{ outlets: { sub: [subRouteName] } }]"
Run Code Online (Sandbox Code Playgroud)
完整示例:
HTML
<div class="tabs tinyscroll">
<button *ngFor="let tab of tabs"
[routerLink]="[{ outlets: { sub: [tab.name] } }]"
routerLinkActive="selected">
<span>{{ tab.label }}</span>
</button>
</div>
<section>
<router-outlet name="sub"></router-outlet>
</section>
Run Code Online (Sandbox Code Playgroud)
app.module.ts
imports: [
...
RouterModule.forChild([
{
path: 'registers',
component: RegistersComponent,
children: [
{path: 'vehicles', component: VehiclesComponent, outlet: 'sub'},
{path: 'drivers', component: DriversComponent, outlet: 'sub'},
{path: 'bases', component: BasesComponent, outlet: 'sub'},
{path: 'lines', component: LinesComponent, outlet: 'sub'},
{path: 'users', component: UsersComponent, outlet: 'sub'},
{path: 'config', component: ConfigComponent, outlet: 'sub'},
{path: 'companies', component: CompaniesComponent, outlet: 'sub'}
],
canActivate: [AuthGuard]
}
]),
...
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
53213 次 |
| 最近记录: |