【发布时间】:2020-09-08 01:14:05
【问题描述】:
我有一个字符串数组的输入,用于我想从服务器接收的枚举:
enumList = ['somethin','branch','country', 'serviceType', 'thatandthis'];
然后我有一个通用的 http-service 方法,它接受一个 enumList 字符串作为参数,并为该 Enum 服务返回一个可观察的 HttpClient:
this.webApi.getEnumByName('somethin','en').subscribe((res)=>{/*do something*/})
this.webApi.getEnumByName('branch','en').subscribe((res)=>{/*do something*/})...
我不是将两者组合成一个循环
for (const item of this.enumList) {
this.webApi.getEnumByName(item).subscribe((res: any) => {
this.enums[item] = res;
});
}
但这并不好......
我想要一个在所有订阅都解决后只完成一次的订阅,同时保留对关联 item string 的引用
使用从 this.webApi.getEnumByName(item) 返回的 observables 数组,concat 或 forkJoin 将不起作用,因为它们不会保持对响应的关联字符串/键/令牌的引用,例如枚举列表中的字符串。
这些连接的 observable 的最终结果应该是:
{
'somethin':{respopnse...},
'branch':{respopnse...},
'country':{respopnse...},
'serviceType':{respopnse...},
'thatandthis':{respopnse...}
}
打破我的头脑将应用 rxjs 解决方案
【问题讨论】:
标签: rxjs angular8 angular9 rxjs-pipeable-operators rxjs-observables