2016-11-07 78 views
1

我想找到一種方式來顯示特定的組件時,只有一個特定的值。 基本上這是現在是如何尋找路由:Angular2路由匹配特定值

const routes: Routes = [ 
    { 
    path: 'users', 
    component: MainComponent, 
    children: [ 
     { 
     path: ':id', 
     component: DashboardComponent, 
     children: [ 
      { 
      path: '1', 
      component: NeedToRenderThisComponent 
      }, 
      { 
      path: '', 
      component: DashboardComponent 
      }, 
      { 
      path: '', 
      outlet: 'sidebar', 
      component: SidebarComponent 
      } 
     ] 
     } 
    ] 
    } 
]; 

所以,我在找的是用戶點擊時,或在進入「/用戶/ 1」,用戶將看到NeedToRenderThisComponent另一種方式。對於所有其他值,將會有DashboardComponent。

DashboardComponent包含兩個路由器網點

<router-outlet name="sidebar"></router-outlet> 
<router-outlet></router-outlet> 

回答

0

您可以使用一個path: '**'路線,然後添加在Angular 2 dynamic tabs with user-click chosen components自己喜歡顯示的組件。

AFAIK沒有辦法匹配值。在較舊的路由器中存在正則表達式路由,但在當前的路由器中,尚未實現(尚未?)。

更新

自定義路徑的匹配支持紛紛落地並很可能會提供下一個更新 https://github.com/angular/angular/issues/12442

+1

所以基本上thiss會匹配一切,並會呈現組件,然後在部分我會根據url來動態顯示組件,對不對? – user2732627

+0

沒錯,那是我的想法。 –

+0

似乎支持自定義路徑匹配已着陸。看到我更新的答案。 –