1

假设我们有两个接口:

export interface IA{
   something: string;
   myprop: number;
}

export interface IB{
   myprop: number;
}

我有一个方法应该调用从后端返回 IA 对象的端点,然后它应该调用另一个端点,然后将两个结果合并到 IA 对象中。以前我在做这样的事情:

GetA():Observable<IA>{
    return this.httpClient
        .get<IA>('somewhere')
        .concatMap(a=>Observable.combineLatest(
           Observable.of(a), 
           GetB(a)
        ))
        .map([a,b]=>combineSomehowAandB(a,b))
}

但是现在,有了新版本,rxjs我不得不改用 .pipe(operators[]) 。如何用 pipe() 实现相同的功能?我试过这样,但它不起作用:

GetA():Observable<IA>{
    return this.httpClient
        .get<IA>('somewhere')
        .pipe(
           concatMap(a=>[Observable.of(a), GetB(a)]),
           combineLatest(),
           map([a,b]=>combineSomehowAandB(a,b))
         );
}

提前致谢。

4

2 回答 2

1

看起来您只是没有正确地将原始链重写为 RxJS 6:

return this.httpClient.get<IA>('somewhere')
  .pipe(
    concatMap(a => combineLatest(of(a), GetB())),
    map(([a,b]) => combineSomehowAandB(a,b)),
  );

单独使用combineLatest()没有任何参数是没有用的。

于 2018-08-23T08:14:32.593 回答
0

使用of而不是observable.of

GetA():Observable<IA>{
    return this.httpClient
        .get<IA>('somewhere')
        .pipe(
           concatMap(a=> combineLatest(
              of(a), 
              GetB()
           )),
           map([a,b]=>combineSomehowAandB(a,b))
         );
}
于 2018-08-23T08:15:02.403 回答