2016-08-15 131 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