2017-07-31 35 views
0

私はこのように私のルーティングを設定している:ここで角度2子ルート

const routes: Routes = [ 
     { path: '', redirectTo: 'dashboard', pathMatch: 'full' }, 
     { path: 'dashboard', component: HomeComponent }, 
     { 
      path: 'contact', component: ContactComponent, 
      children: [  
      { 
       path: '', 
       component: ContactComponent 
      }, 
      { 
       path: 'list', 
       component: ContactlistComponent 
      },  
      ] 

     }, 
     // { path: 'list', component: ContactlistComponent }, 
     { path: 'configuration/forms', component: FormsComponent } 
     ]; 

は私のリンクです:

<a [routerLink]="/contact/list">List</a> 
<a [routerLink]="/contact">Add New</a> 

だから私は私の連絡先のリンクがオープンになっている両方のリンクをクリックしたとき。ここで

、私はこれを行うとき、それは動作します:

const routes: Routes = [ 
      { path: '', redirectTo: 'dashboard', pathMatch: 'full' }, 
      { path: 'dashboard', component: HomeComponent }, 
      { 
       path: 'contact', component: ContactComponent,    
      }, 
      { path: 'contact/list', component: ContactlistComponent }, 
      { path: 'configuration/forms', component: FormsComponent } 
      ]; 

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

答えて

2

最初の子ルート(空のパスを持つルート)にpathMatch: 'full'を追加する必要があります。そうでない場合は、contact/listも最初のルートと一致します。

const routes: Routes = [ 
{ path: '', redirectTo: 'dashboard', pathMatch: 'full' }, 
{ path: 'dashboard', component: HomeComponent }, 
{ 
    path: 'contact', component: ContactComponent, 
    children: [  
    { 
     path: '', 
     component: ContactComponent, 
     pathMatch: 'full' 
    }, 
    { 
     path: 'list', 
     component: ContactlistComponent 
    },  
    ] 

}, 
// { path: 'list', component: ContactlistComponent }, 
{ path: 'configuration/forms', component: FormsComponent } 
]; 
+0

児ルートを使用することができ、このコード

const routes: Routes = [ { path: '', redirectTo: 'dashboard', pathMatch: 'full' }, { path: 'dashboard', component: HomeComponent }, { path: 'contact', component: ContactComponent,children:ChilddataRoute }, // { path: 'list', component: ContactlistComponent }, { path: 'configuration/forms', component: FormsComponent } ]; 

を使用して親ルートを使用することができます。私は私の質問でこれに答えるコードを更新しました。それは私が親コンポーネントを与えているので動作していなかったので、それが私をそのデフォルトの連絡先コンポーネントに連れて行っていたのです。 –

1

あなたは、あなたはそれがコード内のマイナーチェンジで働いていた別の活字体ファイル

export const ChilddataRoute: Routes = [ 
    { path: '', component: ContactComponent }, 
    { path: '/list', component: ContactlistComponent }, 
]; 
+0

Bimal私はまだこのソリューションを試していませんが、私はそれがルートを管理する方がはるかに簡単になるように、この方法で確かにそれを試してみます。おかげで –

+0

あなたは歓迎です –