2016-08-15 137 views
0

我遇到问题了解如何正确地在路由中执行路由。我想什么来实现:Angular 2(rc5)路由器 - 侧栏导航(子路由)

App 
    -Contacts (view) 
    -News (view) 
    SidebarNavigation (persistent throught news) 
    -InfoOne (child-view of news) 
    -InfoTwo (child-view of news) 

news.component.html

<div class="sidebar-navigation> 
    <a routerLink="/news/info-one" routerLinkActive="active">Info one</a> 
    <a routerLink="/news/info-two" routerLinkActive="active">Info two</a> 
</div> 

<router-outlet></router-outlet> 

现在,我遇到的问题是,每当我点击info-one/info-two侧边导航栏中消失。如何分辨角度以显示子女<router-outlet></router-outlet>内的子女观点,而不是主要的<router-outlet></router-outlet>

我已经创建了一个叉plunker出官方教程。 看看:app/news.component.tsapp/app.routing.ts


更多的代码:
app.routing.ts

import { Routes, RouterModule } from '@angular/router'; 

import { ContactComponent } from './contact.component'; 
import { NewsComponent, InfoOneComponent, InfoTwoComponent } from './news.component'; 

const appRoutes: Routes = [ 
    { 
    path: '', 
    redirectTo: '/news', 
    pathMatch: 'full' 
    }, 
    { 
    path: 'contact', 
    component: ContactComponent 
    }, 
    { 
    path: 'news', 
    component: NewsComponent 
    }, 
    { 
    path: 'news/info-one', 
    component: InfoOneComponent 
    }, 
    { 
    path: 'news/info-two', 
    component: InfoTwoComponent 
    } 
]; 

export const routing = RouterModule.forRoot(appRoutes); 

感谢

回答

3

问题与Routes,更新如下,

您已经定义了儿童路线和i t需要进入儿童阵列。当你不添加孩子时,它将在主要的router-outlet中加载,而不是你在新闻组件中添加的那个。

const appRoutes: Routes = [ 
{ 
    path: '', 
    redirectTo: '/news/info-one', 
    pathMatch: 'full' 
}, 
{ 
    path: 'contact', 
    component: ContactComponent 
}, 
{ 
    path: 'news', 
    component: NewsComponent, 
    children : [ 
    { 
    path: '', 
    redirectTo: '/news/info-one', 
    pathMatch: 'full' 
    }, 
    { 
    path: 'info-one', 
    component: InfoOneComponent 
    }, 
    { 
    path: 'info-two', 
    component: InfoTwoComponent 
    } 
] 
}]; 

更新了Plunker!!

希望这有助于!

+0

'children'就是我一直在寻找的东西:)非常感谢,虽然看起来你已经搞砸了一下,它只显示信息一。我已经更正了它,如果可以的话,请在您的帖子中更新以备将来参考。 [updated-plunker](http://plnkr.co/edit/g4UTf82IBtZh6DmVoYLf?p=preview)另外,你不需要额外的东西:编号的东西。 – Baki

+0

感谢在答案中添加了更新的plunker,我实际上正在尝试一些东西并没有意识到,我正在更新的答案中添加了plunker。 –

+0

对我有帮助,谢谢 – Kamruzzaman