1

我有一個角度4.3.6應用程序與延遲加載模塊。下面是部分根路由器:這兩個例子模塊中路由器死循環第二canActivate在惰性加載模塊上的保護

const routes: Routes = [ 
    { path: '', redirectTo: 'fleet', pathMatch: 'full' }, 
    { 
    path: '', 
    component: AppComponent, 
    canActivate: [AuthenticationGuard], 
    children: [ 
     { 
     path: 'fleet', 
     loadChildren: "./modules/fleet.module", 
     canActivate: [AuthenticationGuard] 
     }, 
     { 
     path: 'password/set', 
     loadChildren: "./modules/chooseNewPassword.module", 
     canActivate: [ChoosePasswordGuard] 
     } 
    ] 
    } 
] 
// Exports RouterModule.forRoot(routes, { enableTracing: true }); 

我的孩子路由器:

艦隊:

RouterModule.forChild([ 
    { 
    path: '', 
    component: FleetComponent, 
    canActivate: [AuthenticationGuard] 
    } 
]); 

選擇新密碼:

RouterModule.forChild([ 
    { 
    path: '', 
    component: ChooseNewPasswordComponent, 
    canActivate: [ChoosePasswordGuard] 
    } 
]); 

AuthenticationGuard電話一個看起來像這樣的方法:

return this.getUserSession().map((userSession: UserSession) => { 
    if (userSession && userSession.ok) { 
    return true; 
    } 
    else if (userSession && userSession.expired) { 
    this.router.navigate(['password/set']) 
     .catch((e: Error) => console.error(e)); 
    return true; 
    } 
    else { 
    window.location.replace('/'); 
    return false; 
    } 
} 

因此,如果用戶的會話正常,它將激活路由。如果用戶的密碼已過期,則會將用戶重定向到選擇新密碼模塊。如果沒有會話,則重定向到登錄。

ChoosePasswordGuard做了類似的事情,但不僅保護了選擇新的密碼組件(不同的設備用於一般設置密碼):

return this.getUserSession().map((userSession: UserSession) => { 
    if (userSession) { 
    return userSession.expired; 
    } 
    else { 
    return false; 
    } 
}); 

該模塊拆分工作過。

現在,我被困在重定向循環中。隨着路由器追蹤,我觀察以下順序。在用戶登錄並AuthenticationGuard校正重定向到/密碼/設置模塊,並且被越區切換到ChooseNewPasswordGuard

  1. NavigationStart(ID:4,URL: '/密碼/設定')
  2. RoutesRecognized {ID:4,URL: 「/密碼/設置」,urlAfterRedirects: 「/密碼/設置」,狀態:RouterStateSnapshot}
  3. GuardsCheckStart {ID:4,URL: 「/密碼/設置」, urlAfterRedirects:UrlTree,狀態:RouterStateSnapshot}
  4. GuardsCheckEnd {ID:4,URL: 「/密碼/設置」,urlAfterRedirects:UrlTree,狀態:RouterStateSnapshot,shouldActivate:真}
  5. NavigationCancel {ID:4,URL:「/密碼/組「,reason:」「}

然後重複此循環。

(也重複,如果我更換整個ChooseNewPasswordGuard與return Observable.of(true);

編輯:我重定向到根頁面(/),即使我提供的地址欄/#/password/set ......

問題:

  1. 我做了什麼錯在我的路由器(S)或警衛現在模塊是懶的裝逼這個循環?我特別困惑shouldActivate: true其次NavigationCancel reason: ""

  2. 是否有事可做的事實,我在AuthenticationGuard直接重定向,而現在,這個保護被加到我的主要空的根路徑({ path: '', redirectTo: 'fleet', pathMatch: 'full' })它總是叫和重定向,甚至一度我已經設定了路徑?

  3. 我是否需要在我的孩子路線和我的根路線中重複使用canActivate警衛?

  4. 像往常一樣,歡迎任何其他意見。

回答

2

的問題是,我是過度應用AuthenticationGuard:它不應該被應用到頂級AppComponent,因爲它總是會重定向到選擇新密碼模塊,即使當加載一個模塊。

我的根routes應該是這個樣子的:

const routes: Routes = [ 
    { path: '', redirectTo: 'fleet', pathMatch: 'full' }, 
    { 
    path: '', 
    component: AppComponent, 
    // canActivate: [AuthenticationGuard], // <-- Remove this guard 
    children: [ 
     { 
     path: 'fleet', 
     loadChildren: "./modules/fleet.module", 
     canActivate: [AuthenticationGuard] 
     }, 
     { 
     path: 'password/set', 
     loadChildren: "./modules/chooseNewPassword.module", 
     canActivate: [ChoosePasswordGuard] 
     } 
    ] 
    } 
] 

(我歡迎並愉快地接受更好的解釋或更好AuthenticationGuard模式。)

相關問題