我怎样才能捕获超时请求

Fra*_*nel 1 javascript ionic2 ionic3 angular

这是我的代码:

properties.service.ts

get(stringParams) {
    let headers = new Headers({
      "X-AUTH-APIKEY":  API_KEY
    });

    this.options = new RequestOptions({
      headers: headers
    });
    return this.http
      .get(`${API_URL}/properties/?${stringParams}`, this.options)
      .timeout(50)
      .map((response: Response) => response.json())
  }



posts.ts

    this.propertiesService.get(arrayParams).subscribe((data: any) => {

    }), (err) => { console.log("timeoout")};
Run Code Online (Sandbox Code Playgroud)

我把50放在我的超时中因此被解雇但我无法以这种方式捕获错误

}), (err) => { console.log("timeoout")};
Run Code Online (Sandbox Code Playgroud)

我收到此错误:

TimeoutError {name: "TimeoutError", stack: "TimeoutError: Timeout has occurred?    at new Time…http://localhost:8100/build/polyfills.js:3:10143)", message: "Timeout has occurred"
Run Code Online (Sandbox Code Playgroud)

编辑:

this.propertiesService.get(arrayParams).subscribe((data: any) => {

        }), (err) => { console.log("timeoout")};

get(stringParams) {

    return this.http
      .get(`${API_URL}/properties/?${stringParams}`, this.options)
      .timeout(50)
      .map((response: Response) => response.json())
      .catch(this.handleError);
}

private handleError(error: any) { 
  return Observable.throw(error);
}
Run Code Online (Sandbox Code Playgroud)

我试过这个解决方案,但我仍然得到这个错误在此输入图像描述

解:

代码应该是这样的(订阅中的错误):

}, (err) => { console.log("timeoout")});
Run Code Online (Sandbox Code Playgroud)

Ant*_*e V 5

err 应该在里面 subscribe

this.propertiesService.get(arrayParams).subscribe((data: any) => {

    }, (err) => { console.log("timeoout")});
Run Code Online (Sandbox Code Playgroud)

你应该catch例外:

get(stringParams) {

    return this.http
      .get(`${API_URL}/properties/?${stringParams}`, this.options)
      .timeout(50)
      .map((response: Response) => response.json())
      .catch(this.handleError);
}

private handleError(error: any) { 
  return Observable.throw(error);
}
Run Code Online (Sandbox Code Playgroud)