首页 > 解决方案 > Console.Log 在 RXJS Tap 操作符内不执行

问题描述

我有以下可观察的:

this.authenticationService.isSignedIn() -> Observable<Boolean>

this.user$ -> Observable<UserModel>

我需要检查基于两者的条件,所以我尝试了:

zip(this.authenticationService.isSignedIn(), this.user$).pipe(
  map(([isSignedIn, user]: [boolean, UserModel]) => isSignedIn && user.claims))
); 

因为我得到了一个意想不到的结果,所以我尝试使用以下方法检查:

zip(this.authenticationService.isSignedIn(), this.user$).pipe(
  tap(([isSignedIn, user]: [boolean, UserModel]) => {
    console.log(isSignedIn);
    console.log(user);
  })
);

但两人console.log没有被处决。我错过了什么?

标签: angulartypescriptrxjsangular10

解决方案


你可能会错过订阅。所有 rxjs 的东西都是惰性的,在订阅完成之前不会运行。如果您不需要以其他方式处理结果,除了您的tap操作员,只需.subscribe()在最后添加

zip(...).pipe(
  tap(...)
).subscribe();

推荐阅读