Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

520
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda