我有这个服务,最初返回 null(初始化后台作业)[this.q = true]
。但稍后如果我将其更改为 [ this.q = false]
并且如果作业成功它将返回值。
因此,首先我需要调用 [this.q = true]
并重试 [this.q = false]
。
但是下面的代码不起作用。它总是使https://api.example.com?q=true
return this.http.get('https://api.example.com?q=' + this.q)
.map((res: any) => {
if (res === null) {
throw new Error("not enough tiles !");
}
return res;
}).catch((e) => {
this.q = false;
return error;
})
.retryWhen(e => e.delay(2000))
.retry(3);
最佳答案
Retry
重新订阅源 Observable,所以,如果你想在 retry
上更改 get
请求的参数,你应该使源可观察的参数值部分的计算。例如
return Observable.of('https://api.example.com?q=')
.concatMap((baseUrl) => this.http.get(baseUrl + this.q))
.map((res: any) => {
if (res === null) {
throw new Error("not enough tiles !");
}
return res;
}).catch((e) => {
this.q = false;
return error;
})
.retryWhen(e => e.delay(2000))
.retry(3);
关于angular - 我可以更改 Rxjs Retry 上的查询参数值吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51060758/