Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/33.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Angular AuthGuard中使用@ngrx/store元素_Angular_Rxjs_Ngrx_Ngrx Store_Auth Guard - Fatal编程技术网

在Angular AuthGuard中使用@ngrx/store元素

在Angular AuthGuard中使用@ngrx/store元素,angular,rxjs,ngrx,ngrx-store,auth-guard,Angular,Rxjs,Ngrx,Ngrx Store,Auth Guard,我试图在authguardService中使用ngrx/store中的元素。 当路由到达检查canActivate的点时,它工作正常,但当元素更改时,路由保护不会更改其输出,canActivate函数仍然返回通过,即使它应该返回false canActivate功能的代码: canActivate(): Observable<boolean> | Promise<boolean> | boolean { return this.store.pipe(

我试图在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)
    );
  }
正如您在上面的代码中所看到的,我定义了两次路线/优惠,一次是针对Besucher,另一次是针对Lernender 不幸的是,即使在存储中将account.role更改为LERNENDER之后,它仍然与路由匹配,因为BesucherGuardService似乎没有意识到account.role变量的更改


我如何解决这个问题?

当你在你的可观察管道中使用
take(1)
时,它会在第一次发出值后终止订阅。基本上这就是我想要的,不是吗?如果我没有弄错的话,应该在路由文件中每次遇到AuthGuard操作时激活CanActivate。这意味着
CanActivate()
将只返回一次,总计。不是每个电话…我不明白你的意思。那么,您认为编写canActivate函数的正确形式是什么呢?试着简单地从可观察管道中删除
take(1)
操作符。
{
        path: '',
        canActivate: [BesucherGuardService],
        children: [
          { 
            path: 'offers',
            component: BesucherTestComponent
          }
        ]
      },
      {
        path: '',
        canActivate: [AuthGuardService, AgbGuardService, LernenderGuardService],
        children: [
          {
            path: 'offers',
            component: LernenderTestComponent
          }
        ]
      },