I am new in Angular2 and I am learning to make http calls.
I have found Angular2 has these two modules:
HttpModule from @angular/http
and
HttpClientModule from @angular/common/http
and they both support http calls.
However I don't figure out the difference between them.
Does anyone know about this?
The HttpClientModule is a service module provided by Angular that allows us to perform HTTP requests and easily manipulate those requests and their responses. It is called a service module because it only instantiates services and does not export any components, directives or pipes.
"HttpClient is an evolution of the existing Angular HTTP API, which exists alongside of it in a separate package...". The tutorial uses HttpModule and angular.io/guide/http uses HttpClientModule and neither explains when one or the other should be used or what version of Angular is needed to use what.
As of Angular 4.3. x and above, HttpClientModule is available. In the future HttpModule with its Http class will be deprecated.
The HttpClient is used to perform HTTP requests and it imported form @angular/common/http. The HttpClient is more modern and easy to use the alternative of HTTP. HttpClient is an improved replacement for Http. They expect to deprecate Http in Angular 5 and remove it in a later version.
HttpClient
is a new API that came with 4.3, it has updated API's with support for progress events, json deserialization by default, Interceptors and many other great features. See more here https://angular.io/guide/http
Http
is the older API and will eventually be deprecated.
Since their usage is very similar for basic tasks I would advise using HttpClient since it is the more modern and easy to use alternative.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With