2016-07-28 11 views
1

美好的一天。我無法理解帶兒童的新路線如何在Angular 2 rc 4中工作。 我想路由到TestComponent,它有孩子,但是出現錯誤「無法匹配任何路線」test'「出現。我做這樣的路由:this.guide.navigate(['test']);。 這是我聲明我的路線:有孩子的路線不能匹配,導航時

const routes: RouterConfig = [ 
    { 
     path: '', 
     component: LogInComponent 
    }, 
    { 
     path: 'pin',  
     component: PinComponent, }, 
    { 
     path: 'test', 
     component: TestComponent, 
     children: [ 
     { 
      path: '/:page', 
      component: FoodComponent 
     } 
     ] 
    } 
] 

export const mainRouterProviders = [ 
    provideRouter(routes) 
]; 

我不能在默認情況下使用一些頁面的測試(如「路徑:‘/’」),因爲它的小孩都喜歡的標籤,並在TestComponent的構造他們的名單裝載。

回答

2

有幾件事情

  • 添加pathMatch: 'full',到第一路徑,否則例如/ping會爲孩子航線ping搜索在LogInComponent

  • 加缺省路由test重定向到一些默認ID或一些虛擬組件。 Angular不喜歡組件是否有<router-outlet>,但沒有路徑來添加組件。

const routes: RouterConfig = [ 
    { 
     path: '', 
     pathMatch: 'full', 
     component: LogInComponent 
    }, 
    { 
     path: 'pin',  
     component: PinComponent, }, 
    { 
     path: 'test', 
     component: TestComponent, 
     children: [ 
     { 
      path: '', 
      pathMatch: 'full', 
      redirectTo: 'myDefaultId' 
      // or 
      // component: DummyComponent 
     }, 
     { 
      path: '/:page', 
      component: FoodComponent 
     } 
     ] 
    } 
]; 
+0

它的工作!謝謝。有沒有辦法不使用默認路由? – Amelina

+0

據我所知不。 –