链式可观测,但倾听第二个可观测的变化



我正在输送两个可观测

this.obs1$
.pipe(
skip(1),
mergeMap(schedules => {
this.schedules=schedules
for (let elm in schedules) {
....
}
return this.selectedDate$;
})
)
.subscribe(selectedDate => {
....

问题是如果CCD_ 1未被触发,则订阅不发送数据。

即使Obs1$没有改变,但selectedDate$应该始终在Obs1$之后运行,我也希望获得selectedDate$

您可以使用combineLatest

combineLatest([
this.obs1$,
this.selectedDate$
]).subscribe(([obs1Value, selectedDate]) => {...})

现在,每次更改obs1$selectedDate$时,都会触发订阅。

但请记住,只有当两个可观察器都至少发射一个值时,这才会起作用。如果你想确保订阅被触发,即使你根本不确定this.obs1$会被触发,你也可以用Obs1$0管道来链接它,这将确保发出一个值。

this.obs1$.pipe(startWith('defaultValue'))

最新更新