Angular 6 服务中的多个 map() 调用
Multiple map() calls in an Angular 6 service
我有一个 HTTP GET 请求 returns 我想将多个对象转换为多个可观察对象。以下是响应示例:
{
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
}
]
}
这是我的服务,它获得了两个可观察值:
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"]));
方法一
创建 2 个将在请求解决后更新的主题。
let map1 = new Subject();
let map2 = new Subject();
this.lookup1 = map1.pipe(map(response => response["lookup1"]));
this.lookup2 = map2.pipe(map(response => response["lookup2"]));
this.apiService.get('/lookups/').subscribe( response => {
map1.next(response);
map2.next(response);
})
方法二
您可以使用 concatMap
和 from
将一个流转换为另一个流。
this.apiService.get('/lookups/').pipe(
concatMap( responseJson => from(Object.values(responseJson)))
).subscribe( arrayElement=> console.log(arrayElement))
输出:
// first object emitted :
[
{
"label": "lookup1 option 1",
"value": 1
},
{
"label": "lookup1 option 2",
"value": 2
}
]
// second object emitted :
[
{
"label": "lookup2 option 1",
"value": 1
},
{
"label": "lookup2 option 2",
"value": 2
}
]
concatMap
获取一个 Observable 并发出另一个 Observable。
来自
将可迭代元素转换为流。您将获得与 iterable 中的项目一样多的排放量。
我有一个 HTTP GET 请求 returns 我想将多个对象转换为多个可观察对象。以下是响应示例:
{
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
}
]
}
这是我的服务,它获得了两个可观察值:
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"]));
方法一
创建 2 个将在请求解决后更新的主题。
let map1 = new Subject();
let map2 = new Subject();
this.lookup1 = map1.pipe(map(response => response["lookup1"]));
this.lookup2 = map2.pipe(map(response => response["lookup2"]));
this.apiService.get('/lookups/').subscribe( response => {
map1.next(response);
map2.next(response);
})
方法二
您可以使用 concatMap
和 from
将一个流转换为另一个流。
this.apiService.get('/lookups/').pipe(
concatMap( responseJson => from(Object.values(responseJson)))
).subscribe( arrayElement=> console.log(arrayElement))
输出:
// first object emitted :
[
{
"label": "lookup1 option 1",
"value": 1
},
{
"label": "lookup1 option 2",
"value": 2
}
]
// second object emitted :
[
{
"label": "lookup2 option 1",
"value": 1
},
{
"label": "lookup2 option 2",
"value": 2
}
]
concatMap 获取一个 Observable 并发出另一个 Observable。
来自 将可迭代元素转换为流。您将获得与 iterable 中的项目一样多的排放量。