gpt4 book ai didi

angular - 多个命名路由器导出 Angular 2

转载 作者:太空狗 更新时间:2023-10-29 16:57:59 26 4
gpt4 key购买 nike

我想通过子路由显示不同的 View 。例如,我希望我的模板包含多个路由器 socket

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.

最佳答案

当前版本的多重命名 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'
}
];
Home 组件中的

模板:

<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

另见

关于angular - 多个命名路由器导出 Angular 2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38038001/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com