2017-08-13 47 views
1

我有一個路徑列表,我想將CanActivate應用於除某些路徑之外的所有路徑。是否有任何方法可以放棄某個路徑。 目前我正在將canActivate應用於所有網址。如果網址很多,我們不希望將其應用於所有網址,我會將其應用於父路徑。 但是,如果我們適用於家長,它也適用於所有的孩子。有什麼方法可以丟棄一些孩子嗎?如何將CanActivate應用於除某些路徑之外的所有路徑

const routes: Routes = [ 
    { path : '', component: UsersListComponent, canActivate:[AuthenticationGuard] }, 
    { path : 'add', component : AddComponent, canActivate:[AuthenticationGuard]}, 
    { path : ':id', component: UserShowComponent }, 
    { path : 'delete/:id', component : DeleteComponent, canActivate:[AuthenticationGuard] }, 
    { path : 'ban/:id', component : BanComponent, canActivate:[AuthenticationGuard] }, 
    { path : 'edit/:id', component : EditComponent, canActivate:[AuthenticationGuard] } 
]; 
+0

嘿,這個解決方案適合你嗎? –

回答

0

這是不可能丟棄一些孩子,但你可能會破壞你的路由在衛隊適用於部分,而不是一些這樣的方式,

下面嘗試,

{ 
    path: 'parentPath', 
    component: ParentComponent, 
    canActivate: [AuthGuard], 
    children: [ 
     {path : 'child1', component: ChildComponent1}, 
     {path : 'child2', component: ChildComponent2} 
    ] 
    }, 
    { 
    path: 'parentPath', 
    component: ParentComponent, 
    children: [ 
     {path : 'child3', component: ChildComponent3} 
    ] 
    } 

如果您設計上述路線,則Gurads僅適用於child1child2,而不適用於child3

通過這種方式,您可以輕鬆地將父母級別的Guard應用於某些孩子。

這是Plunker !!

希望這有助於!

相關問題