2017-07-31 167 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

它的工作與代碼的微小變化。我在我的問題中更新了這個答案中的代碼。它沒有工作,因爲我已經給出了父組件,這就是爲什麼它把我帶到那個默認的聯繫組件。 –

1

您可以使用父路由使用此代碼

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 } 
     ]; 

您可以使用兒童路線與其他打字稿文件

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

Bimal我還沒有嘗試過這個解決方案,但我會嘗試,因爲通過這種方式管理路線會容易得多。謝謝 –

+0

不客氣 –