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?
Asked
Active
Viewed 2,962 times
7
-
2The second part could be a new question – hayden Jan 06 '18 at 04:25
-
1Do you think I should edit out the second part and make another post? – theMobDog Jan 06 '18 at 05:15
1 Answers
16
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.

Sahil Purav
- 1,324
- 4
- 19
- 32
-
That explains it. Tentatively marked it as the best answer. Gonna wait a little and see if anyone wanna fill in on the 2nd question. (or make another post for that). Thank you. – theMobDog Jan 06 '18 at 05:31
-
1
-
Thanks for the answer but then why is the address same for both, i mean why doesn't the import statement for "`HttpClient`" look like this " `import { HttpClient } from '@angular/common/http/HttpClientModule'` " – DaniyalAhmadSE Aug 26 '20 at 11:15