1

为什么不调用内部可观察的运算符tap和?应该订阅它进入的可观察对象,对吗?为什么这个订阅不会触发那些运营商?mapcombineLatestobsArr

const obsArr = [];

[[1, 2], [3, 4], [5, 6]].map(arr => {

  const observable = from(arr);

  observable.pipe(
    tap(item => {
      // this is NOT called
      console.log('tap', item)
    }),
    map(item => {
      // this is NOT called
      return item * -1;
    })
  );

  obsArr.push(observable);
});

combineLatest(obsArr).subscribe(latestValues => {
  console.log(latestValues);
  // LOG: [2, 4, 5]
  // LOG: [2, 4, 6]
});

工作堆栈闪电战: https ://rxjs-y2h4rn.stackblitz.io

感谢您的解释!

4

1 回答 1

4

问题是您将管道添加到可观察对象,但将原始可观察对象推送到数组。相反,您应该推送修改后的 observable:

[[1, 2], [3, 4], [5, 6]].map(arr => {

  const observable = from(arr);

  obsArr.push(observable.pipe(
    tap(item => {
      console.log('tap', item)
    }),
    map(item => {
      return item * -1;
    })
  ));
});
于 2020-03-12T22:26:18.117 回答