【问题标题】:Why operators (tap, map) are not called on inner observable, when using combineLatest?为什么在使用 combineLatest 时不会在内部 observable 上调用运算符(点击、地图)?
【发布时间】:2020-06-25 00:03:43
【问题描述】:

为什么不调用内部 observable 的运算符 tapmapcombineLatest 应该订阅它在 obsArr 中获得的 observables,对吗?为什么这个订阅不会触发那些操作符?

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

感谢解释!

【问题讨论】:

    标签: javascript typescript rxjs rxjs-pipeable-operators rxjs-observables


    【解决方案1】:

    问题是您将管道添加到可观察对象,但将原始可观察对象推送到数组。相反,您应该推送修改后的 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;
        })
      ));
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-07-30
      • 1970-01-01
      • 2018-05-17
      相关资源
      最近更新 更多