我在 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/