7

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?

theMobDog
  • 1,251
  • 3
  • 13
  • 26

1 Answers1

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
    Better to have another post for 2nd question. :) – Sahil Purav Jan 06 '18 at 05:48
  • 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