我有一个函数,需要从两个独立的异步函数(返回可观测值)获得2个响应。1. Func1返回可观测性2. Func2返回可观测性--它们不依赖于彼此--它们可以单独运行。功能3应该有一些如何执行Func1和Func2的结果。我正在使用RXJS,并尝试使用管道和flatMap或地图,但仍然没有成功。
发布于 2019-07-31 05:02:24
为此你需要forkJoin。这里试一试:
import { Component } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { forkJoin } from 'rxjs';
@Component({
selector: 'my-app',
templateUrl: './app.component.html',
styleUrls: [ './app.component.css' ]
})
export class AppComponent {
constructor(private http: HttpClient) {}
getGoogle() {
return this.http.get('https://api.github.com/users/google');
}
getMicrosoft() {
return this.http.get('https://api.github.com/users/microsoft');
}
ngOnInit() {
forkJoin(
this.getGoogle(),
this.getMicrosoft()
)
.subscribe(
res => console.log(res)
)
}
}
这是一个供您参考的Working Sample StackBlitz。
https://stackoverflow.com/questions/57291210
复制相似问题