首页 > 解决方案 > 尝试使用 ngIf 订阅 observable 并进入循环

问题描述

这是用户登录时应该显示的图标的html:

<mat-icon class="logout-icon logout-spacer" *ngIf="(isLoggedIn() | async)" (click)="logout()" >exit_to_app</mat-icon>

该图标的组件如下所示

  isLoggedIn(){
    return this.authService.isLoggedIn().pipe(
      take(1),
      map((authState) => !!authState),
      tap(user =>{
        if (user){
          console.log("user found");
        } else {
          console.log("user not found");
        }
      })
    );
  }

还有一个身份验证服务,它的代码显示在下面,由 AngularFire 库支持

isLoggedIn(){
    return this.afAuth.authState.pipe(first());
  }

当我在开发者控制台中运行应用程序时,我只能看到“找不到用户”反复显示。

提前致谢!

标签: angularrxjsangularfire2

解决方案


推荐阅读