28
import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { FormsModule } from '@angular/forms';

import { ClinicFacilityService } from './apiClient.module';


@NgModule({
  imports: [
    CommonModule,
    FormsModule
  ],
  declarations: [
    DailyScheduleComponent,
  ],
  providers: [
    ClinicFacilityService
  ],
  exports: [
    DailyScheduleComponent
  ],
})
export class ClinicDashboardModule {
}

I need to import ClinicFacilityService that is declared in another module (apiClient.module)

Is this even possible, if not why is not possible. At the moment i am importing ClinicFacilityService like this:

import { ClinicFacilityService } from './api-client-service/clinic-facility.service';
Günter Zöchbauer
  • 623,577
  • 216
  • 2,003
  • 1,567
Allan Kimaina
  • 347
  • 2
  • 4
  • 11
  • This is kinda old post but for the sake of others, I find this [article](https://medium.com/@michelestieven/organizing-angular-applications-f0510761d65a) very comprehensive and fully answers this question. Hint: see **Importing services** section of that article. – Aldee Nov 01 '19 at 15:36
  • The answer to this depends on how the service is injected in the service file itself – Aiden Cullo Jul 28 '23 at 00:36

1 Answers1

38

Adding the module to imports should do

import { ApiClientModule } from './apiClient.module';

@NgModule({
  imports: [
    ApiClientModule,
    CommonModule,
    FormsModule
  ],
  declarations: [
    DailyScheduleComponent,
  ],
  exports: [
    DailyScheduleComponent
  ],
})
export class ClinicDashboardModule {
}

otherwise import the file that contains the service class

import { ClinicFacilityService } from './clinic-facility.service';

There is a clear distinction between @NgModule() imports and TypeScript imports.

If you need to use the class name (ClinicFacilityService) then a TypeScript import of that class is required. This is entirely unrelated to @NgModule()

@NgModule({
  ...
  providers: [
    ClinicFacilityService
  ],

If the @NgModule() import is required, then the class name of the module class (ApiClientModule) requires a TypeScript import because the module needs to be passed.

@NgModule({
  imports: [
    ApiClientModule,
  ],
  • TypeScript imports are to to uniquely identify a class.
  • NgModule imports are to define that a module depends on another module.
Günter Zöchbauer
  • 623,577
  • 216
  • 2,003
  • 1,567