首页 > 解决方案 > Angular 6 RXJS 过滤器等待订阅中的布尔值

问题描述

我正在尝试检查用户是否已通过身份验证,并且我正在使用一种方法,如果用户已通过身份验证,则该方法返回 true 或 false 的可观察值。我正在尝试做的是等待授权有一个值,然后在回调函数中检查它是真还是假,并根据它的值执行操作。所以基本上我不希望我的过滤器函数检查值是真还是假,而是在继续订阅回调中的代码之前检查是否有值。

这是我的功能:

this.oidcSecurityService.getIsAuthorized().pipe(filter(authorized => authorized !== undefined)).subscribe(authorized => {
  console.log(authorized);
  this.isAuthorized = authorized;
  if (!authorized) {
    debugger
    this.login()
  }
  else {
    debugger
    this.getBoSpar()
  }
});

我做错了什么,如何让过滤器功能检查是否已获取授权,而不是它是真还是假?我没有收到任何错误,只是授权总是被评估为假,即使我知道用户已经过身份验证(我已经分离了发生在点击事件上的逻辑,并且有效)。

标签: angularrxjsobservable

解决方案


authorized !== undefined看起来不对

authorized != null仅检查 null 和 undefined

this.oidcSecurityService.getIsAuthorized()
  .pipe(
    tap(authorized => console.log('BEFORE', authorized)),
    filter(authorized => authorized != null),
    tap(authorized => console.log('AFTER', authorized))
  )
  .subscribe(authorized => {
    console.log(authorized);
    this.isAuthorized = authorized;
    if (!authorized) {
      debugger
      this.login()
    } else {
      debugger
      this.getBoSpar()
    }
});

值得使用抽头检查过滤器前后通过管道的值。


推荐阅读