错误静态注入器:StaticInjectorError(AppModule)[GameService -> HttpClient]
Error Static Injector: StaticInjectorError(AppModule)[GameService -> HttpClient]
我对 Angular 中的图书馆有疑问。我已经完成了一个库,我在其中使用 HttpClient 询问 API 但是有一个问题,当我尝试从这个库 GameService 继续 class 时,我收到:"Error Static Injector: StaticInjectorError(AppModule)[GameService -> HttpClient]"。当我看到它时,我尝试在我的应用程序中导入 HttpModule 并添加提供者 HttpClient 但没有任何改变。
这是我的 class 库的代码:
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
) {}
尝试从提供者那里删除 GameService,因为您有 providedIn: 'root'
。那里也不需要 HttpClient
好的,所以问题是我尝试在本地安装这个库,但是当我在 NPM 上发布时,它起作用了。显然,当我尝试在本地安装时出现问题。
我对 Angular 中的图书馆有疑问。我已经完成了一个库,我在其中使用 HttpClient 询问 API 但是有一个问题,当我尝试从这个库 GameService 继续 class 时,我收到:"Error Static Injector: StaticInjectorError(AppModule)[GameService -> HttpClient]"。当我看到它时,我尝试在我的应用程序中导入 HttpModule 并添加提供者 HttpClient 但没有任何改变。 这是我的 class 库的代码:
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
) {}
尝试从提供者那里删除 GameService,因为您有 providedIn: 'root'
。那里也不需要 HttpClient
好的,所以问题是我尝试在本地安装这个库,但是当我在 NPM 上发布时,它起作用了。显然,当我尝试在本地安装时出现问题。