【问题标题】:How to set http call timeout in angularjs 4?如何在angularjs 4中设置http调用超时?
【发布时间】:2018-08-22 20:10:45
【问题描述】:

我在 angularjs 4 官方页面 (https://angular.io/guide/http) 中看到设置 http 调用超时但我没有找到任何参考。 有人试过设置吗?

谢谢

【问题讨论】:

  • 我要了 angular 4。谢谢
  • Angular 4 和 Angular 2 一样,依赖于 RxJS。您为什么不至少阅读并尝试答案?
  • var response = this.http.get(API.root + path, options).timeout(3000, new Error('timeout exceeded')).map((res : Response)=> this .checkResponse(res));我有这个错误:“Observable”类型上不存在属性“超时”。

标签: angular ionic3


【解决方案1】:

如果您使用的是 RxJS 版本 6 及更高版本,则当前语法如下:

import { timeout } from 'rxjs/operators';
...
getUsers() {
   return this.http.post(API_URL, {headers: Myheaders})
      .pipe(
          timeout(5000) //5 seconds
      );
} 

参考:https://rxjs-dev.firebaseapp.com/api/operators/timeout

【讨论】:

    【解决方案2】:

    有一个超时操作符:

    getUsers() {
       return this.http.post(this.baseUrl + "users", {headers: Myheaders})
             .timeout(3000, new Error('timeout exceeded'))
             .map(res => res.json());
    } 
    

    【讨论】:

    • var response = this.http.get(API.root + path, options).timeout(3000, new Error('timeout exceeded')).map((res : Response)=> this .checkResponse(res));我有这个错误:“Observable”类型上不存在属性“超时”。
    • 尝试添加 import "rxjs/add/observable‌​/timeout";到您的进口。 Rxjs 需要专门导入大部分函数。
    • 是的,解决方案是 import { Observable } from 'rxjs/Rx';谢谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-01-07
    • 2016-01-12
    • 2014-08-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-29
    相关资源
    最近更新 更多