importing an ES5 class into typescript with angular8 throws NullInjectorError: StaticInjectorError

importing an ES5 class into typescript with angular8 throws NullInjectorError: StaticInjectorError

GameService, UniqueIdService, GridService 是我在模块中使用的服务。

问题:core.js:5847 错误错误:未捕获(承诺): NullInjectorError:StaticInjectorError(App4pcModule)

我经历过这些:

  1. 未捕获的承诺错误静态注入器错误应用程序模块 ==> 做 不适用于我的情况。

  2. ==> 试过了,没有用。

  3. https://github.com/angular/angular/issues/20339 ==> 几乎与我无关

@Injectable()
export class GameService {
    public currentScore: Observable<number>;
    public highScore: Observable<number>;
    public tiles: Observable<ITile[] >;
    public gameOver: Observable<boolean>;
    public won: Observable<boolean>;

    constructor(private gridService: GridService, private store:
        Store<any>) {
    const store$ = store.select<IGame>("game");
    this.tiles = store$.pipe(map(({ tiles }: IGame) =>
        tiles));
    this.currentScore = store$.pipe(
        map(({ currentScore }: IGame) => currentScore)
    );
    this.highScore = store$.pipe(map(({ highScore }: IGame) =>
        highScore));
    this.gameOver = store$.pipe(map(({ gameOver }: IGame) =>
        gameOver));
    this.won = store$.pipe(map(({ won }: IGame) => won));
}
...
}

@NgModule({
    declarations: [TileComponent, MessageComponent,
        GridComponent],
    imports: [BrowserModule, StoreModule.forRoot({ game:
        gameReducer })],
    providers: [GridService, UniqueIdService, GameService]
})
export class AppModule {}
Problem : core.js:5847 ERROR Error: Uncaught (in promise):
NullInjectorError: StaticInjectorError(App4pcModule)

    `stack trace on chromium`:
core.js:5847 ERROR Error: Uncaught (in promise): NullInjectorError: StaticInjectorError(App4pcModule)[Game48Component -> GameService]:
StaticInjectorError(Platform: core)[Game48Component -> GameService]:
NullInjectorError: No provider for GameService!
    NullInjectorError: StaticInjectorError(App4pcModule)[Game48Component -> GameService]:
StaticInjectorError(Platform: core)[Game48Component -> GameService]:
NullInjectorError: No provider for GameService!
    at NullInjector.push../node_modules/@angular/core/fesm5/core.js.NullInjector.get (core.js:1225)
at resolveToken (core.js:1463)
at tryResolveToken (core.js:1407)
at StaticInjector.push../node_modules/@angular/core/fesm5/core.js.StaticInjector.get (core.js:1311)
at resolveToken (core.js:1463)
at tryResolveToken (core.js:1407)
at StaticInjector.push../node_modules/@angular/core/fesm5/core.js.StaticInjector.get (core.js:1311)
at resolveNgModuleDep (core.js:18446)
at NgModuleRef_.push../node_modules/@angular/core/fesm5/core.js.NgModuleRef_.get (core.js:19135)
at resolveDep (core.js:19506)
at resolvePromise (zone.js:852)
at resolvePromise (zone.js:809)
at zone.js:913
at ZoneDelegate.push../node_modules/zone.js/dist/zone.js.ZoneDelegate.invokeTask (zone.js:423)
at Object.onInvokeTask (core.js:24328)
at ZoneDelegate.push../node_modules/zone.js/dist/zone.js.ZoneDelegate.invokeTask (zone.js:422)
at Zone.push../node_modules/zone.js/dist/zone.js.Zone.runTask (zone.js:195)
at drainMicroTaskQueue (zone.js:601)
at
ZoneTask.push../node_modules/zone.js/dist/zone.js.ZoneTask.invokeTask [as invoke] (zone.js:502)
at invokeTask (zone.js:1693)

当没有模块提供您的服务时会发生这种情况。在 app.module 中提供您的服务或更改您的服务以包含此

@Injectable({
  providedIn: 'root'
})

export class YourService { .....