【发布时间】:2017-07-25 12:42:52
【问题描述】:
因为我听说 Angular 的路由器支持嵌套的
(父路由工作正常) app-routing.module.ts
...
const routes: Routes = [
{ path: '', pathMatch: 'full', component: LoginComponent },
{ path: 'login', pathMatch: 'full', component: LoginComponent },
{ path: 'dashboard',
pathMatch: 'full',
component: DashboardComponent // this holds the second router-outlet
},
{ path: '**', component: LoginComponent }
];
@NgModule({
imports: [
RouterModule.forRoot(routes)
],
exports: [ RouterModule ]
})
export class AppRoutingModule { }
app.component.ts - 这只是包含路由器插座,并且在顶层可以正常工作......
<router-outlet></router-outlet>
仪表板包含通用页眉、页脚、侧边栏等。这就是我希望它在顶级路由器插座中的原因。子路由器出口将填充子视图,但不填充。
尝试 1 dashboard-routing.module.ts
export const dashboardRoutes: Routes = [
{ path: '', pathMatch: 'full', redirectTo: 'home' },
{ path: 'home', pathMatch: 'full', component: HomeComponent },
{ path: 'about', pathMatch: 'full', component: AboutComponent }
]
@NgModule({
imports: [
RouterModule.forChild(dashboardRoutes)
],
exports: [ RouterModule ]
})
export class DashboardRoutingModule { }
按照Angular2 : Multiple Router-Outlets & Router-Outlets Inside Child Route尝试2个dashboard-routing.module.ts
export const dashboardRoutes: Routes = [
{
path: 'dashboard',
children:[
{ path: '', component: DashboardComponent},
{ path: 'home', component: HomeComponent},
{ path: 'about', component: AboutComponent}
]
}
]
@NgModule({
imports: [
RouterModule.forChild(dashboardRoutes)
],
exports: [ RouterModule ]
})
export class DashboardRoutingModule { }
在仪表板模板中,不会填充此嵌套的路由器插座。取而代之的是 app.component.html 中的顶级路由器插座 :(
dashboard.component.html
<header>...</header>
<aside class="sidenav">...<aside>
<!-- why can't I populate you? -->
<router-outlet></router-outlet>
****************** 回答,非常感谢PierreDuc! **************
app-routing.module.ts
// seems counter-intuitive that the dashboard component isn't actually in here..., but it works!
const routes: Routes = [
{ path: '', pathMatch: 'full', component: LoginComponent },
{ path: 'login', pathMatch: 'full', component: LoginComponent },
{ path: '**', component: LoginComponent }
];
@NgModule({
imports: [
RouterModule.forRoot(routes),
DashboardRoutingModule // this is the magic. I'm assuming to put it second is better (though putting it first didn't seem to have any immediate negative effect)
],
exports: [ RouterModule ]
})
export class AppRoutingModule { }
dashboard-routing.module.ts
export const dashboardRoutes: Routes = [
{
path: 'dashboard',
component: DashboardComponent,
children:[
{ path: '', component: HomeComponent },
{ path: 'home', pathMatch: 'full', component: HomeComponent },
{ path: 'about', pathMatch: 'full', component: AboutComponent }
]
}
]
@NgModule({
imports: [
RouterModule.forChild(dashboardRoutes)
],
exports: [ RouterModule ]
})
export class DashboardRoutingModule { }
以下是从根目录导航的方法:
login.component.ts
... passed validation ...
this._router.navigate(['/dashboard']);
this._router.navigate(['/dashboard/home']);
或通过仪表板中的侧边栏通过 routerLink 路由 仪表板.component.html
[routerLink]="['../login']" <!-- back to login, though the '../' seems less than ideal
或在仪表板视图中通过 routerLink 到子路由器插座: 仪表板.component.html:
[routerLink]="['../about']"
【问题讨论】:
标签: angular angular2-routing router angular2-template