I am instantiating MyService in app.module.ts, and providing it throughout my code.
However, I want MyService to use httpClient, and I am unable to angular-idiomatically instantiate httpClient to pass as a parameter to MyService. I'm not sure what the right way to access httpClient in MyService is.
I've looked at instantiating httpClient directly and then passing it as a parameter to my service. However it seems this creates a circular dependency. I have also tried messing with Injectors but this is specifically advised against by the Angular team apparently. I strongly feel I am missing something simple.
app.module.ts
imports: [
...
httpClientModule
...
],
function MainServiceFactory() {
return new MyService();
}
...
providers: [{
provide: MyService,
useFactory: MainServiceFactory
}],
...
MyService.ts
...
constructor(private http : HttpClient) {
...
}
Without instantiation and passing as parameter of httpClient: I get an "error in app.module.ts, expected 1 argument", naturally! With instantiation I break angular recommendations.
Edit: I had neglected to specify that I did import the httpClientModule
Before you can use the HttpClient, you need to import the Angular HttpClientModule. Most apps do so in the root AppModule.....
– Roxana