通过发出 http 请求获取路由解析器的数据,这样第二个请求的结果取决于第一个请求的结果

Getting data for route resolver by making http requests such that the result of second one depends on the result of first one

我的路由模块中有解析器

{
    path: 'path1',
    component: FirstComponent,
    resolve: {
     allOrders: DataResolver
    }

}

然后在我的 resolve 函数中有

resolve(): Observable<Array<string>> {

    return this.serviceA.getAllfooNames()
    .map(result=> {

         /* result is an array of strings*/
         return this.serviceB.getAllBarNames(result[0])

         /*orders is also supposed to be an array of strings*/     
          .map(orders=> return orders)                        
    });

 }
}

我希望根据 allOrders 键存储值 orders。 我想将 orders 数组作为 ActivatedRoute 快照中的数据传递。请帮助。

您可以混合使用 concatMapzip:

resolve(): Observable<Array<string>> {
  return this.serviceA.getAllfooNames().pipe(
    concatMap((names) => 
      zip(...names.map((name) => this.serviceB.getAllBarNames(name)))
    ),
    map((...names) => 
      names.reduce((acc, curr) => acc.concat(curr), [])
    ) 
  ); 
}

这将 return 来自 serviceB 调用的所有字符串 return 在一个大的字符串数组中。

基本上它的作用是调用 getAllfooNames,然后 concatMap 等待此请求完成,return 是字符串中的一堆名称。之后,您可以使用 zip 运算符选择它们。该运算符使用数组 map 方法执行传入的所有可观察对象,并在所有操作完成后发出。

然后在地图中拾取它,它接收多个字符串数组作为参数。然后你使用 reduce 把它放在一个大数组中。