2016-08-17 75 views
4

我想在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調用時將觸發反饋服務。

超時啓動時如何啓動服務?

回答

5

假設_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); 
+0

對不起。我回答了這個問題,然後意識到我會誤解這個問題。我已經更新了答案。 – cartant

+0

除了'rxjs/add/operator/timeoutWith'之外,我還需要導入哪些內容才能使其工作? (即「Rx。」部分)。 –

+0

您將需要'import「rxjs/add/observable/defer」'和'import「rxjs/add/observable/throw」'。 – cartant