I have a problem with a library in Angular. I have done a library where I ask an API with HttpClient but there's a problem, when I try to go on a class from this library, GameService, I receive: "Error Static Injector: StaticInjectorError(AppModule)[GameService -> HttpClient]". When I saw that, I tried to import HttpModule inside my application and add in providers HttpClient but nothing changed. Here's the code of my class library:
import { Injectable } from '@angular/core';
import { HttpHeaders, HttpClient } from '@angular/common/http';
import { Game } from './model/game';
import { Observable } from 'rxjs';
import { EndPointGetterService } from '../utilities/end-point-getter.service';
@Injectable({
providedIn: 'root'
})
export class GameService {
private gameUrl = localStorage.getItem('endpoint') + '/Games';
private headers: HttpHeaders;
constructor(private http: HttpClient, private EPGetter: EndPointGetterService) {
this.headers = new HttpHeaders({
'Access-Control-Allow-Origin': 'http://localhost:4200',
'Access-Control-Allow-Methods': 'GET,PUT,POST,DELETE,OPTIONS',
'Access-Control-Allow-Headers': '*',
});
}
getStateGame(groupName: string): Observable<number> {
return this.http.get<number>(this.EPGetter.getEndPointUrl() + '/Games/' + groupName + '/State', { headers: this.headers });
}
And inside my application I have this inside my app.module.ts:
import { HttpClientModule, HttpClient } from '@angular/common/http';
import { GameService } from '@oneroomic/oneroomlibrary';
@NgModule({
declarations: [
AppComponent,
NavComponent,
LockscreenComponent,
SettingsComponent
],
imports: [
HttpClientModule,
],
providers: [
HttpClient,
GameService
],
and that's how i inject the GameService:
constructor(
private snackBar: MatSnackBar,
private gameService: GameService
) {}
Try removing GameService from providers as you have providedIn: 'root'
. And HttpClient is not needed there too
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With