Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

517
Views
Why do need to import HttpClientModule in app.module.ts when I've already imported in a separate module where it is used

I have 2 modules (AppModule and BookModule), BookModule's component injected a service which uses HttpClient so I imported HttpClientModule in it. But application still needs to import HttpClientModule in AppModule.
As per my understanding, we need import module where it is needed instead in RootModule.

here is the CodeSanbox link

over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

If you import any module like HttpClientModule in AppModule then components, services, etc, of the imported module will be accessible from every component of your application.

BTW, may be you forgot to import BookModule in AppModule and for this reason you still need to import HttpClientModule in AppModule though you already imported HttpClientModule in BookModule. However, if BookModule is imported inside AppModule you don't have to import HttpClientModule in AppModule.

over 4 years ago · Santiago Trujillo Report

0

I checked your code, you haven't import your Book Module within App Module, Please modify your app.module.ts like below

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';

import { AppComponent } from './app.component';
import { AppRoutingModule } from './app-routing.module';
import { PageNotFoundComponent } from './not-found/not-found.component';
import { BookModule } from './book/book.module'; // Import Book Module

@NgModule({
  declarations: [AppComponent, PageNotFoundComponent],
  imports: [
    BrowserModule, 
    AppRoutingModule, 
    BookModule // Add your Module inside the imports array
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule {}

Now it will work fine without error

I also have attached the screenshot enter image description here

over 4 years ago · Santiago Trujillo Report

0

as per @Eliseo comment, i've clear my doubt about provider.

if the service provided in root scope than its dependency need to be imported in appmodule.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!