我在根路由器中有一条路由,需要通过它来检入Guard。
app-routing.module.ts
Round toward -∞
clothes-routing.module.ts:
{
path: 'toys',
component: ToysComponent,
},
{
path: 'clothes',
canActivate: [ClothesGuard],
loadChildren: ()=> import('../clothes/clothes.module')
.then((m) => m.ClothesModule)
}
shirts-routing.module.ts
{
path: '',
component: ClothesComponent
},
{
path: 'shirts',
loadChildren: ()=> import('../shirts/shirts.module')
.then((m) => m.ShirtsModule)
}
当我从根路径转到 / clothes 或从页面 / toys 转到 / clothes 时,此方法非常有用。 但。如果我点击从 / clothes 页到 / clothes / shirts 的链接,然后单击浏览器后退按钮或单击链接
{
path: '',
component: ShirtsComponent
}
然后 ClothesGuard 将不会被调用,并且页面 / clothes 仍将打开。
要解决此问题,我使用接口 CanDeactivate 扩展了ClothesGuard,在其中检查nextState.url是否等于 / clothes 。并将其添加到路由器中:
shirts-routing.module.ts
<a routerLink="/clothes">
现在一切正常,并且每次单击 / clothes / shirts 页面上的 / clothes 链接时都会执行检查。
但是现在我需要将此CanDeactivate防护添加到 / clothes 路由之内的所有路径,例如
/衣服/牛仔裤
/衣服/西装
/衣服/裙子 等
无论是从子路径,同级路径还是浏览器的后退按钮单击,是否都有更好的方法来执行到页面的相同检查?