Angular2 Module: How can i import a service from another module

前端 未结 2 379
无人及你
无人及你 2020-12-17 08:53
import { NgModule } from \'@angular/core\';
import { CommonModule } from \'@angular/common\';
import { FormsModule } from \'         


        
相关标签:
2条回答
  • 2020-12-17 09:20

    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.
    0 讨论(0)
  • 2020-12-17 09:30

    This is kinda old post but for the sake of others, I find this article very comprehensive and fully answers this question. Hint: see Importing services section of that article.

    0 讨论(0)
提交回复
热议问题