加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

Angular 2 Http超时

发布时间:2020-12-17 17:58:58 所属栏目:安全 来源:网络整理
导读:我在HTTP超时发布自定义错误消息时遇到问题. 这是一个简单的例子: return this._http.get(url).timeout(5000,new Error("Error message")); 我看到每个人都使用新的错误(“错误消息”),但我收到错误: Error function expects type Scheduler. I’m getting
我在HTTP超时发布自定义错误消息时遇到问题.

这是一个简单的例子:

return this._http.get(url).timeout(5000,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只接受两个参数:

> due:number |日期
> 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);

});

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读