I\'d like to use angular HttpClient
in static method or class (in class it can\'t be defined as constructor parameter).
I tried something like:
Passing the need service/object as a parameter helps a lot. In addition, it helps testing and code "readability". This following solution works with any type of object you are trying to inject. And, at least, you inject it where/when needed. The calling object is responsible for injecting the needed object.
export class SomeNotInjectableService {
static doSomething(injected: any) {
httpClient = injected as HttpClient;
if(httpClient) {
httpClient.get(...);
}
}
}
then in your calling component or service, use it like this
...
export class MyService/*or MyComponent*/{
constructor(private http: HttpClient){}
doTheThing(){
SomeNotInjectableService.doSomething(this.http)/*...subscribe()*/;
}
}