首页 > 解决方案 > 我需要像 combineLatest 这样的东西,但是当任何可观察的发射时

问题描述

所以我的代码是:

combineLatest([obs1, obs2]).subscribe((x => {
  console.log(x)
})

当 EITHER observable 发出时,应该触发 log 语句。但是,文档中有一个问题:

在每个 observable 发出至少一个值之前, combineLatest 不会发出初始值

即使原始可观察对象之一从未发出,我也需要它发出。我该怎么做?很多谢谢

标签: angularrxjs

解决方案


每个可观察对象的典型用法 startWith

combineLatest([
   obs1.pipe(startWith(null)),
   obs2.pipe(startWith(null)]
   ).subscribe((x => {
      console.log(x)
   })

推荐阅读