2016-04-05 7 views
2

親コンポーネントには、組み込みのトップナビゲーション、別のサイドバーnavコンポーネント、メインコンポーネントの<router-outlet>を格納するメインコンテンツdivが含まれています。私は親の主要コンポーネントのルータ出口を変更するために子供のナビゲーションコンポーネント内のリンクを持つ方法について頭を悩ますことを試みています。親コンポーネントのルーティングを制御するAngular2子コンポーネント

`` `

Main Component 
@Component({ 
    selector: 'app', 
    templateUrl: 'mainComponent.html', 
    encapsulation: ViewEncapsulation.None, 
    directives: [ROUTER_DIRECTIVES,ChildSidenav], 
}) 
@RouteConfig([ 
    { path: '/', component: HomeCmp, as: 'Home' }, //for top nav 
    { path: '/about', component: AboutCmp, as: 'About' }, //for top nav 
    { path: '/user', component: UserCmp, as: 'User'}, //for top nav 
    { path: '/data/...', component: SidenavBasicUsage, as: 'Data'}, //link in child component 
    { path: '/dates', component: SelectDatesComponent, as: 'Dates'}, //link in child component 
    { path: '/edit/paths', component: EditPathsComponent, as: 'EditPaths'}, //link in child component 
    { path: '/edit/filters', component: EditFiltersComponent, as: 'EditFilters'}, //link in child component 
]) 
export class AppCmp {} 

Child Component 
@Component({ 
    selector: 'left-control', 
    templateUrl: 'sidebar.component.html', 
    moduleId: module.id, 
    directives: [ROUTER_DIRECTIVES], 
}) 
@RouteConfig([ 
    {path: '/',name: 'Data',component: SelectDataComponent,useAsDefault: true}, 
    {path: '/dates',name: 'Dates',component: SelectDatesComponent}, 
    {path: '/edit/paths',name: 'EditPaths',component: EditPathsComponent}, 
    {path: '/edit/filters',name: 'EditFilters',component: EditFiltersComponent} 
]) 
export class SideNavComponent{} 

` ``

+0

どのようにナビゲートしますか? 'router.navigateXxx()'または ''? –

答えて

1

私はすべてのこれらのメソッドを試していないが、私は、彼らが動作するはずだと思う:[routerLink]="..."

import {Router} from 'angular2/router' 

... 

constructor(private router:Router) {} 

someMethod() { 
    this.router.parent.navigate(['About']); 
    this.router.navigate(['/About']); 
    this.router.navigate(['../About']); 
} 

それは同じように動作します(最初の例を除く)

+0

ルータはどこから来たのですか?私は 'angular2/router'からインポートしようとしましたが、そこにはありません。 – blacknight811

+0

答えを更新しました。私は ':Router'の代わりにコンストラクタ':router'にタイプミスがありました。 –

+0

私は '' someMethod'''メソッドをトリガする子ナビゲーションコンポーネントのclickイベントを使ってそれを行うことができました。あなたがこれをやることを失うことの1つは '' router-link-active''クラスで、 '' routerLink'''を使うとあなたはボックスを出します。 – blacknight811

関連する問題