与以前的Http相比,Angular 5 HttpClient有哪些优势? [英] What are advantages of Angular 5 HttpClient over previous Http?
问题描述
我阅读了官方的升级指南,上面写着因为HttpClient得到广泛采用,因此我们决定..." 但是HttpClient带来的真正好处是什么?
I read official upgrade guide and it says something like "because HttpClient gets wide adoption we decided to..." but what are real benefits this HttpClient brings along?
我当时正在考虑尝试,但是由于我不知道升级后这些东西会发生什么而感到困惑:
I was considering to try it out, but got confused half way as I don't know what needs to happen to these after I upgrade:
从"@ angular/http"导入{Http,标头,RequestOptions,ResponseContentType};
import { Http, Headers, RequestOptions, ResponseContentType } from '@angular/http';
我试图在新客户端中找到一个匹配项: 从'@ angular/common/http'导入{HttpClient,HttpParams,HttpHeaders}
I tried to find a "match in the new client: import { HttpClient, HttpParams, HttpHeaders } from '@angular/common/http'
但是不知道应该如何适当地升级"这些内容,因为指南对此一无所知.
But have no idea how those should be properly "upgraded" as the guide says nothing about it.
第二个问题-我们将如何处理其他http内容?
推荐答案
HttpClient接口与旧的Http接口几乎没有什么变化.最大的区别在于HttpClientModule对中间件(即HTTP拦截器)具有更好的支持.
The HttpClient interface is pretty much unchanged from the old Http interface. The big difference is that HttpClientModule has better support for middleware (i.e .HTTP interceptors).
某些功能优点:
• Strongly typed response body access
• JSON assumed by default (no more need to do .map(t=>t.json())
• Better support for interceptors as middleware
• Immutable request/response objects
• Progress events for request upload/response download
有了新的http客户端,它可以直接将HTTP拦截器用于中间件组件.
With the new http client, its straight-forward to use HTTP interceptors for middleware components.
中间件模式的一些好处:
Some benefits of the middleware pattern:
- HTTP请求的全局错误处理
- 全局重试机制(即重试3次)
- 用于长期运行的全局HTTP微调器.
例如,客户端代码(使用HttpClient)无需担心错误处理,重试或可视化等待组件-所有这些都在一个地方处理.
For example, client code (using HttpClient) does not need to worry about error handling, retries, or visual wait components – all of that is handled in one place.
这篇关于与以前的Http相比,Angular 5 HttpClient有哪些优势?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!