Angular 2 Http超时

标签 angular angular2-http

我在 HTTP 超时时发布自定义错误消息时遇到问题。

这是一个简单的例子:

return this._http.get(url).timeout(5000, new Error("Error message"));

我看到每个人都使用 new Error("Error message") 但我收到错误:

Error function expects type Scheduler. I'm getting this error: Argument of type 'Error' is not assignable to parameter of type 'Scheduler'. Property 'SchedulerAction' is missing in type 'Error'

最佳答案

在 rxjs 4 中,可以通过这种方式自定义错误消息。然而,在最新版本的 rxjs 5 中,timeout 只接受两个参数:

  • 截止日期:数量 |日期
  • scheduler:IScheduler(这是为了管理 Observable 如何处理超时)

如果你想自定义你的错误,你可以尝试这样的事情:

return this._http.get(url)
 .timeout(5000)
 .catch(err => {

   if (err.name !== "TimeoutError") {
      return Observable.throw("Timeout has occurred");
   }

   return Observable.throw(err);

});

关于Angular 2 Http超时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41465687/

相关文章:

angular - npm 上的 angular-cli 和 @angular/cli 有什么区别

php - 如何使用 Http.post (Angular 2) 发布 json 对象(php 服务器端)

angular - 可观察 map 功能未运行(Angular2,http)

ios - 在 iOS 上运行 ionic 应用程序时出现 NSURLConnection 错误 1100

angular - 拦截来自 Angular2 的所有 HTTP 请求

Angular 2 : Chain http requests with concat()

angular - 为 Angular 2 http 请求设置基本 url

angular - 同时使用 ngFor 和 ngIf 列出项目

c# - 将 ASP.net Angular 应用程序部署到 IIS 时出现问题

node.js - Angular 8项目不可用