因为我听说Angular的路由器支持嵌套< router-outlet>标签,我试图使用其中两个。我使用最新的Angular-CLI,从ui-router转换为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.tsexport 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>
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 { }
仪表板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']);
或通过路由器链接通过仪表板中的侧栏进行路由 dashboard.component.html
[routerLink]="['../login']" <!-- back to login, though the '../' seems less than ideal
或通过routerLink到子路由器插座的仪表板视图中: dashboard.component.html:
[routerLink]="['../about']"
答案 0 :(得分:11)
子router-outlet
中填充了路由器配置中的children
数组。但是还有重复。您应该移除dashboard
中的AppRoutingModule
条目,然后转到尝试2:
应用程序的路由
const routes: Routes = [
{ path: '', pathMatch: 'full', component: LoginComponent },
{ path: 'login', pathMatch: 'full', component: LoginComponent },
{ path: '**', component: LoginComponent }
];
并保持DashboardRoutingModule
与尝试2(使用子数组)一样,并在AppRoutingModule
内或AppModule
内导入此模块。