Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

528
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda