我在 Angular 6 中有以下两个函数:
http1() {
return this.http.post<any>('/url1').subscribe(() => console.log(1));
}
http2() {
return this.http.post<any>('/url2').subscribe(() => console.log(2));
}
现在,我想使用 zip 订阅两个 Observables,并在完成这两个 Observables 后执行操作:
Observable.zip([http1(), http2()]).subscribe(() => console.log(3));
在此示例中,控制台中未打印 3
,仅打印 1
和 2
。我需要打印其中三个,如何实现?
最佳答案
您必须从这些 http
请求中返回 Observable
数据,而不是在那里订阅
http1() {
return this.http.post<any>('/url1').pipe(tap(val => console.log(1)));
}
http2() {
return this.http.post<any>('/url2').pipe(tap(val => console.log(1)));
}
这些可观察量
不应该包装在数组
中,而应该是参数。
zip(http1(), http2()).subscribe(() => console.log(3));
关于angular - 对 Observable zip 订阅执行操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52292377/