rxjs里withLatestFrom operators的用法

时间:2022-07-28
本文章向大家介绍rxjs里withLatestFrom operators的用法,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

Combines the source Observable with other Observables to create an Observable whose values are calculated from the latest values of each, only when the source emits.

上图说明:从时间轴上看,source Observable emit value a时,没有其他输入的Observable,因此value a被discard;emit b时,其他Observable最新的值为1,因此最后output为b1;对c,d,e来说,另一个Observable最新的值为4,因此最后的值为c4,d4和e4.

看个例子:

const clicks = fromEvent(document, 'click');
const timer = interval(1000);
const result = clicks.pipe(withLatestFrom(timer));
result.subscribe(x => console.log('diablo : ' + x));

输出:

如果我们把另一个Observable emit值也打印出来,就能看得更清楚: