2016-10-22 21 views
1

ルートが「メイン」に変更されたときに読み込まれるコンポーネントがあります。 私はそのルートに 'test'という子ルートを定義しました角度2 - ルーターアウトレット付きの子ルート

ルートがmain/testに設定されている場合、テストコンポーネントをメインコンポーネント内にロードします。メインコンポーネントの内部には、子コンポーネントを表示するためのルータアウトレットがあります。私が持っているhtmlの主要コンポーネントで

import { Route } from '@angular/router'; 
    import { MainComponent } from './main.component'; 
    import { TestComponent } from './test/test.component'; 
    import { SessionGuard } from '../../services/session/session.guard'; 

    export const MainRoutes: Route[] = [ 
     { 
     path: 'main', 
     component: MainComponent, 
     canActivate: [SessionGuard], 
     children: [ 
     { path: 'test', component: TestComponent } 
    ] 
    } 
]; 

:ここ

は私main.routes.tsある

Main 
<router-outlet></router-outlet> 

私は取得していますエラーは次のとおりです。 はメイン/テストルートを照合することはできません

ルート変更をトリガーするボタン:

<button type="button" class="btn btn-default" (click)="changeRoute('main/test');" >Test</button> 

ルートを変更する方法:

changeRoute(route: string) { 
    event.stopPropagation(); 
    this.router.navigate([route]); 
} 

おかげ

+0

、以下に示すように

export const MainRoutes: Route[] = [ { path: 'main', component: MainComponent, canActivate: [SessionGuard], children: [ {path:'',redirectTo:'test', pathMatch: 'full'}, {path: 'test', component: TestComponent } ] 

、それを設定する必要があり、テスト?メインコンポーネントとのデフォルトの子ルートですか? – micronyks

+0

はい、それは子コンポーネントです。メインにはがあります。私はリダイレクトするためにボタンを使用し、アドレスバーだけでも試しました。 – Pete

+0

どのようにボタンの仕組みを投稿してください?あなたのコードは何ですか? – micronyks

答えて

2

router-outletを空にすることはできません。これは、メインコンポーネントにリダイレクトするときに、デフォルトの子ルーターセットがないことを意味します。あなただけの、あなたが設定の下に使用する必要があり、ボタンのクリックイベントでtestにリダイレクトしたい場合は、ちょうどあなたがにリダイレクトんか

export const MainRoutes: Route[] = [ 
     { 
     path: 'main', 
     component: MainComponent, 
     canActivate: [SessionGuard], 
     children: [ 
        {path:'',component:null},  //<<<<### here with null value 
        {path: 'test', component: TestComponent } 
    ]