Closed j0wey closed 6 years ago
Inject the InMemoryDbService in the constructor and cast it to your specific implementation type. This is not pretty but should work.
private myInMemoryDbService : MyInMemoryDbService;
constructor(inMemoryDbService: InMemoryDbService) {
this.myInMemoryDbService = <MyInMemoryDbService>inMemoryDbService;
}
@LambdaCruiser Is exactly right. It is registered in DI under InMemoryDbService
which knows nothing about your extensions. You cast it as suggested to gain TypeScript access to those.
This is how DI works everywhere in Angular. I don't think there is any action to take on this issue and therefore I am closing it.
I added some methods for initialization purposes of the mock data to my implementation of the
InMemoryDbService
and want to access it in a component.As far as I understand,
HttpClientInMemoryWebApiModule
lists my implementation of theInMemoryDbService
, that I hand over in the forRoot method, in its providers array asInMemoryDbService
. So it should be possible to access the Service in other components in the constructor withconstructor(private myInMemoryDataService: InMemoryDbService) {}
and use it withthis.myInMemoryDataService.myInitializationMethod();.
But this fails because of an error that my initialization method is not implemented. If I add my service to the providers array of my root module it works, but then it seems I have to instances of the service. One with my correctly initalized data and one without which is used by the Angular in-memory-web-api.
Is it possible what I want to achieve and if so, how? What do I have to import in my component to get the right instance of the
InMemoryDbService
?