我正在尝试在我的 authguardService 中使用来自 ngrx/store 的元素。当路由到达它检查 canActivate 的点时,它可以正常工作,但是当元素更改时,路由保护不会更改它的输出,并且 canActivate 函数仍然返回,即使它应该返回 false。
canActivate 函数的代码:
canActivate(): Observable<boolean> | Promise<boolean> | boolean {
return this.store.pipe(
select('account'),
map((account) => {
console.log('account', account);
if (account.agb === true) {
return true;
} else {
this.router.navigate(['/agb']);
return false;
}
}),
take(1)
);
}
路由:
{
path: '',
canActivate: [BesucherGuardService],
children: [
{
path: 'offers',
component: BesucherTestComponent
}
]
},
{
path: '',
canActivate: [AuthGuardService, AgbGuardService, LernenderGuardService],
children: [
{
path: 'offers',
component: LernenderTestComponent
}
]
},
正如您在上面的代码中看到的那样,我定义了两次路线/offers,一次用于 Besucher 时,另一次用于 Lernender 不幸的是,即使在 store 中 account.role 更改为 LERNENDER 后,它仍然匹配route 因为 BesucherGuardService 似乎不知道 account.role 变量的变化。
我怎样才能解决这个问题?