angular - 对 Observable zip 订阅执行操作

标签 angular rxjs rxjs6

我在 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,仅打印 12。我需要打印其中三个,如何实现?

最佳答案

您必须从这些 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/

相关文章:

angular - RXJS 如何在另一个中使用一个 observable 的结果(然后将这两个结果一起处理)

angular - 错误 TS2351 : This expression is not constructable. 类型 'typeof import("jspdf")' 没有构造签名

unit-testing - RxJS Redux-Observables 测试 retryWhen inside an epic

rxjs - 如何强制可观察对象按顺序执行?

Java Rx react 器 : conditional doOnNext

angular - RXJS - 多个连续的 http 请求

angular - RXJS如何延迟每个可观察的(http请求)并合并所有请求输出

html - 根据是否扩展动态更改 mat-expansion-panel-header 的背景颜色

angular - husky > pre-commit hook 失败(添加 --no-verify 绕过)

javascript - 正式更改 Angular 4 中的选择框动态