Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
980 views
in Technique[技术] by (71.8m points)

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

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';
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...