zl程序教程

您现在的位置是:首页 >  其它

当前栏目

rxjs里withLatestFrom operators的用法

用法 Rxjs Operators
2023-09-14 09:04:03 时间

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值也打印出来,就能看得更清楚:

要获取更多Jerry的原创文章,请关注公众号"汪子熙":