我希望通过子路径显示不同的视图。例如,我希望我的模板能够容纳多个路由器插座
Version: @angular/router": "3.0.0-alpha.7"
<router-outlet></router-outlet>
<router-outlet name="route1"></router-outlet>
<router-outlet name="route2"></router-outlet>
在我的路由器中,我想指定路由器的名称。
正如我在一个问题中所看到的,解决方法是指定AuxRoute,但此版本中不存在AuxRoute。
{path: '/', component: HomeComponent, as: 'Home'},
new AuxRoute({path: '/route1', component: Route1Component, as: 'Route1'}),
new AuxRoute({path: '/route2', component: Route2Component, as: 'Route2'})
在angular 2官方网站上,我发现可以有多个路由器,但我找不到任何资源。
https://angular.io/docs/ts/latest/guide/router.html
A template may hold exactly one unnamed <router-outlet>. The router supports multiple named outlets, a feature we'll cover in future.
答案 0 :(得分:30)
当前版本多重命名的router-outlet
(对于angular2 RC.6 ^ )如下所示:
路由器配置
const appRoutes: Routes = [{
path: 'home',
component: HomeComponent,
children: [
{ path: '', component: LayoutComponent },
{ path: 'page1', component: Page1Component, outlet: 'route1' },
{ path: 'page2', component: Page2Component, outlet: 'route2' },
{ path: 'page3', component: Page3Component, outlet: 'route3' }
]
}, {
path: 'articles',
component: ArticlesComponent,
children: [
{ path: '', component: LayoutComponent },
{ path: 'article1', component: Article1Component, outlet: 'route1' },
{ path: 'article2', component: Article2Component, outlet: 'route2' }
]
}, {
path: '',
redirectTo: '/home',
pathMatch: 'full'
}
];
主页组件中的模板:
<router-outlet></router-outlet>
<router-outlet name="route1"></router-outlet>
<router-outlet name="route2"></router-outlet>
<router-outlet name="route3"></router-outlet>
来自根组件的导航示例:
constructor(router: Router) {
router.navigateByUrl('/home/(route1:page1//route2:page2//route3:page3)');
}
替代方式:
<a [routerLink]="['/home', { outlets: {'route1':['page1'],'route2': ['page2'] }}]"></a>
这是现场Plunker Example
另见