错误静态注入器:StaticInjectorError(AppModule)[GameService-> HttpClient]

时间:2019-03-04 09:07:15

标签: angular angular-library

我在Angular中遇到一个库问题。我已经完成了一个库,在其中我用HttpClient询问API,但是当我尝试从该库GameService继续上一个类时,出现了一个问题,我收到:“错误静态注入器:StaticInjectorError(AppModule)[GameService-> HttpClient]” 。当我看到该消息时,我尝试将HttpModule导入我的应用程序中并添加提供程序HttpClient,但没有任何更改。 这是我的类库的代码:

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 });
  }

在我的应用程序中,我的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
  ],

这就是我注入GameService的方式:

constructor(
  private snackBar: MatSnackBar,
  private gameService: GameService
  ) {}

2 个答案:

答案 0 :(得分:1)

尝试从提供商中移除GameService,方法是 {{1}} 。而且那里也不需要HttpClient

答案 1 :(得分:0)

好的,所以问题是我尝试在此库的本地安装,但是当我在NPM上发布时,它起作用了。因此,显然,当我尝试在本地安装时会出现问题。