2016-11-22 26 views
2

我有以下的路由配置:重定向到Angular 2中的第一個允許的路線?

const routes: Routes = [ 
    { path: '', redirectTo: 'customers', pathMatch: 'full' }, 
    { path: 'customers', component: CustomerListComponent, canActivate: [CustomerGuard] }, 
    { path: 'products', component: ProductListComponent, canActivate: [ProductGuard] }, 
    { path: 'sales', component: SalesListComponent, canActivate: [SalesGuard] } 
]; 

的問題是,我沒有「dashbord」,只有域管理頁面,雖然每個授權的用戶將有機會獲得其中至少有一個,他們都不會對所有用戶都可用。

有沒有一個選項可以說:重定向到第一條路線,可以被激活?或者我需要在路徑'/'下編寫一個虛擬組件,根據用戶角色進行動態重定向?

回答

1

三是無法重定向到第一條可用路線。 根據角色使用具有虛擬組件的虛擬路由只是重定向的方式。您也可以在後衛https://angular.io/docs/ts/latest/guide/router.html#!#guards中進行重定向,但虛擬路由無論如何都需要一個組件。

什麼,你也可以使用router.resetConfig()像顯示在Dynamic routing based on external data

+0

加載取決於角色的路線所以這是我的想法,我需要創建一個角色,以「虛擬」成分,這將重定向我的地方做,但這個虛擬組件不一定是可視化的(模板等)? –

+0

你可以使用'template:'''。我在每個項目中都有一個'DummyComponent',只是爲了能夠在路由器要求的地方列出組件,而不是由我來做。 AFAIR最近有一個拉動請求,以便在這種情況下不再需要一個組件。 –