在 angular 中顺序发出两个 http 请求,rxjs

Making two http requests sequentially in angular, rxjs

目前我有以下需求:

      return this.registrationService.registratePerson(data).pipe(
        switchMap(
          res => {
            return this.registrationService.sendPassportScan(this.form.value.passportScanCopyFile, res.AisUserIsn);
          }
        )
      );

第二个请求利用了第一个请求中获得的结果,这是我需要的东西,但问题是现在 return第二个请求(内部请求)的结果.是否有可能在 rxjs 运算符的帮助下以某种方式使其 return 成为一个包含来自内部请求的字段以及来自外部请求的字段的可观察对象?

在应用程序中使用了第 5 版的 rxjs。

当然可以。像这样:

return this.registrationService.registratePerson(data).pipe(
    switchMap(
        res => forkJoin(
            of(res), 
            this.registrationService.sendPassportScan(/* relevant args */),
        )))

(生成的流将包含一个 2 元素数组)。

在这种情况下,concatMap 可能会对您有所帮助

https://www.learnrxjs.io/operators/transformation/concatmap.html

示例https://angular-academy.com/rxjs-switchmap-concatmap-mergemap-exhaustmap/#concatmap