gpt4 book ai didi

http - Ionic2 中的多个 $http 请求间隔

转载 作者:可可西里 更新时间:2023-11-01 17:06:09 24 4
gpt4 key购买 nike

我想将多个 http 请求 fork 到一个可观察对象中,按时间间隔调用它并在所有订阅者之间共享相同的数据。

到目前为止,我的代码如下所示:

import {Observable} from "rxjs/Rx";

let obs = Observable
.interval(10000)
.publish()
.connect()
.forkJoin(
this.http.get(API_URL + "x", {headers: this.headers})
.map(response => response.json()),
this.http.get(API_URL + "y", {headers: this.headers})
.map(response => response.json())
)
.map(res => {
// do some stuff
return res;
});

错误:

Typescript Error
Property 'forkJoin' does not exist on type 'Subscription'.

我读过:

https://blog.thoughtram.io/angular/2016/06/16/cold-vs-hot-observables.html

Multiple $http requests in Ionic2

http://restlet.com/blog/2016/04/12/interacting-efficiently-with-a-restful-service-with-angular2-and-rxjs-part-2/

谢谢!

最佳答案

像这样的东西应该可以工作:

let source = Observable
.interval(1000)
.flatMap(() => {
return Rx.Observable.forkJoin(
this.http.get(API_URL + "x", {headers: this.headers})
.map(response => response.json()),
this.http.get(API_URL + "y", {headers: this.headers})
.map(response => response.json())
)
})
.publish();

source.connect();

然后 Observers 订阅这个 observable

source.subscribe(...);

我在这里所做的是获取间隔可观察值并替换所有操作的 forkJoin 的每个值。然后发布它以将相同的数据共享给许多订阅者。

您在实现中遇到的另一个问题是您从 .connect() 返回订阅,这只是为了在您需要时处理它(取消订阅)。观察员应订阅已发布的来源。

关于http - Ionic2 中的多个 $http 请求间隔,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41847067/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com