Angular 4.3 HttpClient响应对象 [英] Angular 4.3 HttpClient Response Obj

查看:76
本文介绍了Angular 4.3 HttpClient响应对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在将服务从Angular 4.3切换到新的HttpClient.

I've been switching my services to the new HttpClient from Angular 4.3.

return this._http.get<ValidateEmailResponse>(`${_validateEmailApi}`, { params })
    .catch(this.handleError);

但是,响应的类型是<Object>,而不是<ValidateEmailResponse>

However, the response is of type <Object> and not of <ValidateEmailResponse>

例如;

应该不像声明的那样吗?

Should it not be as declared?

推荐答案

这是我的代码现在与新的4.3 HttpClient相似的样子(请参见下文).注意我方法的返回类型.

This is what my code now looks like with the new 4.3 HttpClient (see below). Notice the return type of my method.

getProducts(): Observable<IProduct[]> {
    return this._http.get<IProduct[]>(this._productUrl)
        .do(data => console.log('All: ' + JSON.stringify(data)))
        .catch(this.handleError);
}

private handleError(err: HttpErrorResponse) {
    // in a real world app, we may send the server to some remote logging infrastructure
    // instead of just logging it to the console
    let errorMessage = '';
    if (err.error instanceof Error) {
        // A client-side or network error occurred. Handle it accordingly.
        errorMessage = `An error occurred: ${err.error.message}`;
    } else {
        // The backend returned an unsuccessful response code.
        // The response body may contain clues as to what went wrong,
        errorMessage = `Server returned code: ${err.status}, error message is: ${err.message}`;
    }
    console.error(errorMessage);
    return Observable.throw(errorMessage);
}

这篇关于Angular 4.3 HttpClient响应对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆