I have an library with 2 components and 2 services A & B.
2 of the components are using my service A.. no problem there - I can see that A have been created once - I've wrote to the console
in the ctor.
Service B needs service A for his doings, therefor it injects A. and now i see another creation of A (in the console)
service B is doing, The same way as the components are doing.
import { A } from './A'
.
.
.
constructor(private myA: A) {...}
But this time it is creating my service A again. so i have 2 instance.
This is not good for me.. and also weird.. I'm relatively new in the new generation of angular - And all the time struggling with that on small things like this.. It is frustrating.
Anyone ?
UPDATE
I've realised that it happens in my clients app when they have lazy loading for sub module, and importing my module there..
For ex.
partials.modules.ts
@NgModule({ declarations: [..],exports: [...], imports: [ ..., MyModule ]});
export class PartialsModule {}
pages.module.ts -- lazy loaded by the routing
@NgModule({ declarations: [..],exports: [...], imports: [ ..., PartialsModule ]});
export class PagesModule {}
app.module.ts
@NgModule({ declarations: [..],exports: [...], imports: [ ..., PartialsModule ]});
export class AppModule {}
app module is creating an instance of MyMoudle->my service, and PagesModule as well. I guess it is because of the lazy modules which has their own injectors.
But how can i solve it ?
I've tried moving the import of my module to the app.module.ts with the forRoot
static method as explained here. Now the components in the partialModule
fail to find the components in MyModule
(My library)
:(