RxJs Zip 仅从发送的流中获取第一个值

RxJs Zip only takes the first value from the sent stream

我有以下使用 RxJs Zip 运算符的代码:

of(1, 2, 3)
  .pipe(zip(of(5), of(10)))
  .subscribe((a) => console.log(a));

输出将是

[1, 5, 10]

但是在 of(1, 2, 3) 处初始化的值 2 和 3 发生了什么?

内部可观察对象 of(5)of(10) 只有一个元素。静态方法 zip() 仅在所有可观察对象发出相应通知时才会发出。

例如

of(1, 2, 3)
  .pipe(zip(of(5, 6, 7), of(10, 11, 12)))
  .subscribe((a) => console.log(a));

会发射

[1, 5, 10]
[2, 6, 11]
[3, 7, 12]

这里还要强调的是静态方法zip() is deprecated and has since been replaced by the zip()函数。使用新的 zip() 相当于上面的

import { of, zip } from 'rxjs';

zip(
  of(1, 2, 3), 
  of(5, 6, 7), 
  of(10, 11, 12)
).subscribe((a) => console.log(a));