0

Given two modules, ModuleA and ModuleB, in ModuleA we want to use ServiceB from ModuleB. What should be done so the following code is functional?

@NgModule({
        imports: [],
        providers: [ServiceA],
        declarations: [ComponentA],
        bootstrap: [ComponentA]
        )
}
export class ModuleA {}
@NgModule({
        imports: [],
        providers: [ServiceB],
        declarations: [ComponentB],
        bootstrap: [ComponentB]
        )
}
export class ModuleB {}
Milo
  • 3,002
  • 9
  • 25
  • 40
  • check out [import service from another module in angular](https://stackoverflow.com/questions/40396070/angular2-module-how-can-i-import-a-service-from-another-module) – Avinash Jun 06 '18 at 19:07

1 Answers1

3

You need to export the service from ModuleB and add the ModubleB under imports of the ModubleA.

Module A:

@NgModule({
imports:        [ModubleB],
providers:  [ ServiceA ],
declarations:   [ ComponentA ],
bootstrap:      [ ComponentA ]
)}

ModuleB

export class ModuleB { }
@NgModule({
imports:        [ ],
providers:  [ ServiceB ],
declarations:   [ ComponentB ],
bootstrap:      [ ComponentB ]
exports : [ServiceB]
)}
Sajeetharan
  • 186,121
  • 54
  • 283
  • 331