【问题标题】:Observable pipes可观察管道
【发布时间】:2020-01-23 14:42:04
【问题描述】:

我对可观察对象的管道有疑问

假设我有以下代码:

const skip$ = of(4);
const length$ = of(24);
const schoolId$ = of(1);

const source = combineLatest(skip$, length$, schoolId$).pipe(
  map(([skip, length]) => `skip: ${skip}, length: ${length}`),
  map(text => text ) // I need now schoolId, how can i get
);

在第二张地图中,我需要 schoolId。如果不这样做,我如何获得 schoolId:

const source = combineLatest(skip$, length$, schoolId$).pipe(
  map(([skip, length, schoolId]) => ({text: `skip: ${skip}, length: ${length}`, schoolId})),
  map(text => `${text.text}, schoolId: ${text.schoolId}` )
);

您可以在这里尝试stackblitz

【问题讨论】:

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


    【解决方案1】:

    与所有 ReactiveX 一样,您有很多选择。也许最接近你现在的方法是使用withLatestFrom...

    const source = combineLatest(skip$, length$).pipe(
      map(([skip, length]) => (`skip: ${skip}, length: ${length}`)),
      withLatestFrom(schoolId$),
      map(([text, schoolId]) => `${text}, schoolId: ${schoolId}` )
    );
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-16
      • 2021-07-10
      • 1970-01-01
      • 1970-01-01
      • 2020-02-02
      相关资源
      最近更新 更多