I have a shared service between the different modules in my Angular application and I have implemented an Angular library. I want to use the shared service in the library too, i.e. if a data is updated in the consumer I want the library to be updated too.
I tried to use consumer in the app.module
:
providers:[{provide: 'ParameterStorageService', useClass:ParameterStorageService},
in the library:
forRoot(ParameterStorageService) : ModuleWithProviders<any>{
return {
ngModule:CompanyAndDealModule,
providers:[
{provide: 'ParameterStorageService',useClass:ParameterStorageService}]
The problem that the data is not updated in the library when it is modified in the consumer.
As described by @jonrsharpe: "useClass will create a new instance each time", so I changed useClass to useExisting and it works.
providers:[{provide: 'ParameterStorageService', useExisting:ParameterStorageService}