Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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'; 
like image 339
Allan Kimaina Avatar asked Nov 03 '16 07:11

Allan Kimaina


People also ask

How do I run a service from another module?

Another way to invoke a service from another module is to create an export with an SCA binding and then save the export. Locate the export in the Business Integration view, and drag it to another module. An import with a matching SCA binding is created in the second module.

Can a module import another module?

Such a file is called a module; definitions from a module can be imported into other modules or into the main module (the collection of variables that you have access to in a script executed at the top level and in calculator mode).

How do I use a component from another module?

Component can be declared in a single module only. In order to use a component from another module, you need to do two simple tasks: Export the component in the other module. Import the other module, into the current module.

Can two modules import each other in Angular?

Sharing moduleslinkYou can put commonly used directives, pipes, and components into one module and then import just that module wherever you need it in other parts of your application.


1 Answers

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.
like image 86
Günter Zöchbauer Avatar answered Sep 28 '22 22:09

Günter Zöchbauer