2016-09-06 8 views
1

Angular2で新しく、アプリケーションにいくつかのルートを追加しようとしています。私は2つの分離したルーティングファイルがあります。Angular2 RC6の子を持つルート

app.routing.ts

const appRoutes: Routes = [ 
    { 
    path: 'login', 
    component: LoginComponent 
    }, 
    { 
    path: '', 
    component: LoginComponent 
    }, 
    { 
    path: '**', component: My404Component 
    } 
]; 


export const routing = RouterModule.forRoot(appRoutes); 

とdashboard.routing.ts

const appRoutes: Routes = [ 
    { 
    path: 'dashboard', 
    component: DashboardComponent, 
    children: [ 
     { 
     path: 'child', 
     component: ChildComponent, 
     }, 

    ], 
    canActivate: [AuthGuard], 
    } 
]; 


export const dashboardRouting: ModuleWithProviders = RouterModule.forChild(appRoutes); 

私は私が受け取るhttp://localhost:3000/dashboardと404ページエラーにアクセスする場合

を、しかし、もし私は http://localhost:3000/dashboard/childにアクセスしましたページが表示されます。

私はダッシュボード内childrenプロパティを削除した場合、私はhttp://localhost:3000/dashboardにアクセスし、ページを見ることができます...

は私が間違って何をしているのですか?

答えて

1
const appRoutes: Routes = [ 
    { path: '', pathMatch: 'full', component: LoginComponent }, // order matters 
    { path: 'login', component: LoginComponent }, 
    { path: '**', component: My404Component } 
]; 

だけでなく、子ルートにデフォルトルートを追加します。

const appRoutes: Routes = [ 
    { path: 'dashboard', component: DashboardComponent, canActivate: [AuthGuard], children: [ 
     { path: '', redirectTo: 'child', pathMatch: 'full' }, 
     { path: 'child', component: ChildComponent, }, 
    ], 
    } 
]; 
+0

こんにちは、あなたの迅速な答えをありがとう:)。あなたの変更に伴い、404ページは表示されませんが、/ダッシュボードにアクセスしようとすると自動的に/ dashboard/childにリダイレクトされます。 /ダッシュボードにアクセスして「ダッシュボードのテンプレート」を表示する方法はありますか? – jos

+0

'DashboardComponent'に* a *よりも' 'がある場合は、子ルートを提供する必要があります。 'redirectTo'ではなく' path: '' ''子ルートに何も表示しない 'Dummy'コンポーネントを追加することができます。 –

+0

はい!魅力のように働く。非常に役立ちます :) – jos

関連する問題