将三个可观察对象结合在一起,两个使用forkjoin,一个使用mergemap

问题描述 投票:0回答:1

三个api调用,其中两个没有参数可传递。因此,我使用了forkJoin这两个api调用,但是第三个需要从前两个api获取的参数。我不知道如何实现。

到目前为止是我的代码。

ngOnInit();
{
  const categories$ = this.blogService.getCategories(); // one
  const posts$ = this.blogService.getPosts(); // two

  forkJoin([
    categories$,
    posts$
  ]).pipe(
    map(([categories, posts]) => {
      return posts.map(post => {
        ... // doing some stuff here
        return post;
      });
    }),
    mergeMap(posts => {
      return posts.map(post => this.blogService.getFeaturedImage(post.featured_media)); // three
    }, y => console.log(y))
  ).subscribe();
}

在上面的代码中,forkJoin可以正常工作。但是没有打第三通电话。我需要将值传递给第三项服务。怎么做?

P.S y returns the value of second call as expected

angular rxjs rxjs6
1个回答
1
投票

请尝试以下代码,您正在返回一个可观察数组,需要forkJoin(异步)或concat(顺序)来执行它们

 mergeMap(posts => {
      return forkJoin(posts.map(post => this.blogService.getFeaturedImage(post.featured_media))); // three
    })
© www.soinside.com 2019 - 2024. All rights reserved.