gpt4 book ai didi

Angular 4.3 HttpClient 响应对象

转载 作者:太空狗 更新时间:2023-10-29 18:30:49 25 4
gpt4 key购买 nike

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

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

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

比如;

enter image description here

不应该像声明的那样吗?

最佳答案

这就是我的代码现在使用新的 4.3 HttpClient 时的样子(见下文)。请注意我的方法的返回类型。

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 响应对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45595328/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com