在RxJS可插值运算符之间传递数据的最佳方法?

0 rxjs angular rxjs-pipeable-operators

除了要在RxJS的下一个可插入运算符中获取其响应数据之外,我还希望传递URL。您认为实现这一目标的最明智的方法是什么?提前致谢。

这是一个例子。 https://stackblitz.com/edit/angular-rxjs-passing-data-to-next-operator-question

我尝试了一些运算符,但找不到合适的运算符。(实际上,我什至不知道为什么传递返回可观察到mergeMap的函数会导致在下一个运算符中获取数据作为函数的参数...)

from([
  'https://jsonplaceholder.typicode.com/posts',
  'https://jsonplaceholder.typicode.com/comments',
  'https://jsonplaceholder.typicode.com/albums',
])
  .pipe(
    mergeMap(url => this.getData(url)),
    tap(posts => console.log(posts[0])), // I want to get url too here!!
  ).subscribe();
Run Code Online (Sandbox Code Playgroud)

我希望在pipable运算符中将url及其响应数据配对。

mar*_*tin 5

您可以将响应映射到所需的任何内容:

from([
  'https://jsonplaceholder.typicode.com/posts',
  'https://jsonplaceholder.typicode.com/comments',
  'https://jsonplaceholder.typicode.com/albums',
]).pipe(
    mergeMap(url => this.getData(url).pipe(
      map(response => ({ response, url })),
    )),
    tap(response => console.log(response)),
  ).subscribe();
Run Code Online (Sandbox Code Playgroud)