Angular 2 rxjs超时回调
问题描述:
我想在HTTP调用发生超时事件时向用户提供反馈。Angular 2 rxjs超时回调
我尝试这样做:
return this._http
.post(apiUrl + 'Search/Everything', params, {withCredentials: true, headers: this.headers})
.timeout(5000, this._feedbackService.error("Custom error message"))
.map((response: Response) => response.json().data);
但是,只要HTTP调用时将触发反馈服务。
超时启动时如何启动服务?
答
假设_feedbackService.error
返回Error
,你应该能够做你需要与timeoutWith
和运营商什么:
import "rxjs/add/observable/defer";
import "rxjs/add/observable/throw";
import "rxjs/add/operator/timeoutWith";
import { Observable } from "rxjs/Observable";
return this._http
.post(apiUrl + 'Search/Everything', params, {withCredentials: true, headers: this.headers})
.timeoutWith(5000, Observable.defer(() => Observable.throw(this._feedbackService.error("Custom error message"))))
.map((response: Response) => response.json().data);
对不起。我回答了这个问题,然后意识到我会误解这个问题。我已经更新了答案。 – cartant
除了'rxjs/add/operator/timeoutWith'之外,我还需要导入哪些内容才能使其工作? (即“Rx。”部分)。 –
您将需要'import“rxjs/add/observable/defer”'和'import“rxjs/add/observable/throw”'。 – cartant