Why To Import { Httpclientmodule } In App.module.ts
Solution 1:
You should look into how angular modules work. Your app.module.ts contains AppModule which is a root module. Every application has at least one module i.e. root module. If you import any module inside your AppModule then its (imported module) components will be accessible to every component of your application.
Thats why to make HttpClient available "everywhere" in the app:
import the HttpClientModule inside AppModule. Now you can use Services, Components etc defined inside HttpClientModule in your own services or components.
Solution 2:
For the variable type information you use on this line
constructor(private http:HttpClient)
Without that import, there would be an error since that type would be unknown.
Solution 3:
In the Angular docs it says that the HttpClientModule, "Configures the dependency injector for HttpClient with supporting services for XSRF." So it looks like that's why it would be a prerequisite for using the HttpClient.
Post a Comment for "Why To Import { Httpclientmodule } In App.module.ts"