【问题标题】:unable to subscribe multiple observable using withlatestfrom无法使用 withlatestfrom 订阅多个 observable
【发布时间】:2021-05-17 01:25:10
【问题描述】:

我有三个可观察对象,需要将这些值作为参数传递给服务,所以我尝试使用 WithLatestFrom() 来实现这一点,但是当我将所有值都作为可观察对象时它工作正常。

this.payment$.pipe(
withLatestFrom(this.first$, this.second$, this.three$)
).subscribe((v, first, second, three) => {
this.methodInfoService.fetchInfo(v, first, second, three)
})

但问题是,第一个 observable 有时可能未定义,因此我无法订阅其他两个 observale。请建议是否有任何替代解决方案来实现这一目标。

【问题讨论】:

    标签: angular typescript rxjs


    【解决方案1】:

    当你使用 combineLatest/withLatestFrom 时,你必须确保所有的 observable 都发出至少一个值。因此,在这种情况下,解决方案取决于您的期望。您可以为 first$ 设置默认值。例如

    withLatestFrom(this.first$.pipe(startWith('yourDefaultValue')), this.second$, this.three$)
    

    【讨论】:

      猜你喜欢
      • 2017-03-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-01-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-23
      相关资源
      最近更新 更多