angular - 我可以更改 Rxjs Retry 上的查询参数值吗?

标签 angular rxjs rxjs5

我有这个服务,最初返回 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/

相关文章:

javascript - RxJS 不能与 Math.random() 配合使用

javascript - rxjs-tslint 与 rxjs-tslint-rules 包

javascript - RxJS 超时不起作用

Angular 2 : rxjs forkJoin http error continue stream

angular - 从 HttpModule 转换为 HttpClientModule

angular - 如何修复 "Cannot set property of undefined"?

rxjs - 使用 RxJS Observable.concatMap 进行内存管理?

javascript - 如何在没有 Node.JS 的情况下最小化 Angular 的打包?

javascript - 循环 JSON 对象以检查给定值是否存在

angular - 如果输入为空,则禁用按钮