我有一个组件,可以调用一些对不同 url 的 http 请求。每个请求都作为可观察对象实现。
getIds():Observable<any[]> {
return this.http
.post(url1, '')
.pipe(map(({ Ids}: any) => Ids.map(item => ({Id: item.Id }))));
}
getNames(data: Names):Observable<any[]> {
return this.http
.post(url2,element)
.pipe(map(({ Results }: any) => Results[0].map(item => ({id: item.ID, name: item.CVSS}))));
}
从组件中,我需要做,
ngOnInit() {
this.Ids = this.getIds();
Ids.forEach(function(element)
this.getNames();
}
有人可以告诉我如何执行此命令吗?
最佳答案
从 RXJS 导入 switchMap
import { switchMap } from 'rxjs/operators'
然后通过管道传输到返回的可观察到的 map
this.getIds().pipe(
switchMap((id: string) => this.getNames(id))
).subscribe((response) -> {
console.log(response)
})
关于angular - 如何在 Angular 7 中使用管道强制执行 http 请求中的顺序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53217352/