使用angular8将ES5类导入打字稿会引发NullInjectorError:StaticInjectorError

时间:2019-06-17 06:25:55

标签: typescript dependency-injection angular8

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

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

我已经经历过这些:

  1. ERROR Error: Uncaught (in promise): Error: StaticInjectorError(AppModule) 未承诺的错误-staticinjectorerrorappmodule ==> 不适用于我的情况。

  2. https://stackoverflow.com/a/53775775/11584774 ==>尝试过此操作,无济于事。

  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)

1 个答案:

答案 0 :(得分:1)

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

data-radium