Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular 4, difference between httpClient and HttpClientModule

In this article here and others :It looks like you need to import HttpClientModule in app.module.ts and HttpClient in app.component.ts to make a http request. Why? How do they work?

like image 536
theMobDog Avatar asked Jan 06 '18 04:01

theMobDog


People also ask

What is the difference between HttpClient and HttpClientModule in angular?

"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.

What is the difference between HttpModule and HttpClientModule?

They both support HTTP calls but HTTP is the older API and will eventually be deprecated. The new HttpClient service is included in the HttpClientModule that used to initiate HTTP request and responses in angular apps. The HttpClientModule is a replacement of HttpModule.

What is the difference between HTTP and HttpClient?

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.

What is HttpClientModule?

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.


1 Answers

In simple words,

You can call HttpClientModule as a "feature-bundle" that comes as a core part in Angular. In this "feature-bundle", you have many other small features like HttpClient (in Angular language, it's "service"), Interceptors etc.

In order for you to use these "feature(s)", you need to import the "feature-bundle" inside your main module (in your case, Angular's AppModule) so that, you have access to HttpClient and many other services inside your working component.

One liner:

HttpClient is a angular service bundled inside HttpClientModule.

like image 146
Sahil Purav Avatar answered Oct 15 '22 12:10

Sahil Purav