【发布时间】:2018-07-24 14:34:31
【问题描述】:
我有一个 HTTP GET 请求,它返回多个我想变成多个可观察对象的对象。以下是响应示例:
{
lookup1:
[
{
"label": "lookup1 option 1",
"value": 1
},
{
"label": "lookup1 option 2",
"value": 2
}
],
lookup2:
[
{
"label": "lookup2 option 1",
"value": 1
},
{
"label": "lookup2 option 2",
"value": 2
}
]
}
这是我的服务,它有两个 observables:
this.lookup1 = this.apiService.get('/lookups/')
.pipe(map(response => response["lookup1"]));
this.lookup2 = this.apiService.get('/lookups/')
.pipe(map(response => response["lookup2"]));
如何使用一个 HTTP GET 请求完成此操作?
编辑
请注意,这样的代码将执行 2 个 HTTP GET 请求:
let lookups = this.apiService.get('/lookups/');
this.lookup1 = lookups
.pipe(map(response => response["lookup1"]));
this.lookup2 = lookups
.pipe(map(response => response["lookup2"]));
【问题讨论】:
-
that returns multiple objects- 我们在这里讨论了多少个对象?此外,如果有 100 个对象——你打算如何将这 100 个可观察对象暴露给它的消费者——这里的目标是什么? -
目标是通过一次 API 调用获取应用程序所需的所有查找。