簡體   English   中英

Angular2 CanActivate保護除一個以外的所有路線

[英]Angular2 CanActivate guard for all routes except one

我知道我們可以對位於一個模塊中的路線進行分組。 像那樣:

canActivate: [AuthGuard],
    children: [
      {
        path: '',
        children: [
          { path: 'crises', component: ManageCrisesComponent },
          { path: 'heroes', component: ManageHeroesComponent },
          { path: '', component: AdminDashboardComponent }
        ],
      }

但我應該將保護添加到每個模塊的路由文件中。 而且我有很多。

如果他沒有被授權,我希望用戶不能去除除一個(登錄路線)之外的任何路線。

什么是為所有人添加警衛的正確方法?

您可以使用帶有防護的無組件空路徑父路徑

{ path: '', canActivate: [AuthGuard],  children: [
  {
    path: '',
    children: [
      { path: 'crises', component: ManageCrisesComponent },
      { path: 'heroes', component: ManageHeroesComponent },
      { path: '', component: AdminDashboardComponent }
    ],
  }
}

並在警衛中檢查用戶是否已登錄。如果未登錄且當前路由已login則仍允許login

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM