Angular HttpClient:将 HttpResponse 转换为响应 [英] Angular HttpClient: Convert HttpResponse to Response

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

问题描述

在使用 Http 时,我们从 @angular/http 库中获取响应类型为 Response,类似地使用 res 转换为 JSON.json()

While using Http we get response type as Response from @angular/http library, similarly converting to JSON using res.json()

但是通过使用来自 @angular/common/httpHttpClient 我们通过使用 observe 得到直接的 JSON 或 HttpResponse>

But by using HttpClient from @angular/common/http we get direct JSON or HttpResponse by using observe

是否有任何解决方法可以将类型从 Response 转换为 HttpResponse 或以任何其他方式从 HtpClient 获取响应,以便 .json() 会起作用吗?

Is there any work around to convert type from Response to HttpResponse or any other way to get response from HtpClient so that .json() will work?

推荐答案

http 客户端默认会隐式调用 res.json(),您无需自己手动将响应类型设置为 responseTypejson

http client by default calls res.json() implicitly and you don't need to that manually yourself and set the respond type to responseType to json

JSON is an assumed default and no longer needs to be explicitly parsed

新的 Angular HttpClient API

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

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