如何解决“ NullInjectorError:没有为InjectionToken @ ngrx / router-store配置提供程序!”在Angular项目上升级到ngrx v8之后?

时间:2019-06-12 21:47:55

标签: angular ngrx

我刚刚将项目从Angular 6(和ngrx 6)升级到Angular8。我尝试遵循Angular 8的迁移指南,并能够构建所有内容,但是最终在运行它时出现此错误。 :

完整错误消息:

Unhandled Promise rejection: StaticInjectorError(AppModule)[StoreRouterConnectingModule -> InjectionToken @ngrx/router-store Configuration]: 
  StaticInjectorError(Platform: core)[StoreRouterConnectingModule -> InjectionToken @ngrx/router-store Configuration]: 
    NullInjectorError: No provider for InjectionToken @ngrx/router-store Configuration! ; Zone: <root> ; Task: Promise.then ; Value: NullInjectorError: StaticInjectorError(AppModule)[StoreRouterConnectingModule -> InjectionToken @ngrx/router-store Configuration]: 
  StaticInjectorError(Platform: core)[StoreRouterConnectingModule -> InjectionToken @ngrx/router-store Configuration]: 
    NullInjectorError: No provider for InjectionToken @ngrx/router-store Configuration!
    at NullInjector.push../node_modules/@angular/core/fesm5/core.js.NullInjector.get (http://localhost:4200/vendor.js:44694:25)
    at resolveToken (http://localhost:4200/vendor.js:44932:24)
    at tryResolveToken (http://localhost:4200/vendor.js:44876:16)
    at StaticInjector.push../node_modules/@angular/core/fesm5/core.js.StaticInjector.get (http://localhost:4200/vendor.js:44780:20)
    at resolveToken (http://localhost:4200/vendor.js:44932:24)
    at tryResolveToken (http://localhost:4200/vendor.js:44876:16)
    at StaticInjector.push../node_modules/@angular/core/fesm5/core.js.StaticInjector.get (http://localhost:4200/vendor.js:44780:20)
    at resolveNgModuleDep (http://localhost:4200/vendor.js:61915:29)
    at _createClass (http://localhost:4200/vendor.js:61968:32)
    at _createProviderInstance (http://localhost:4200/vendor.js:61932:26) NullInjectorError: StaticInjectorError(AppModule)[StoreRouterConnectingModule -> InjectionToken @ngrx/router-store Configuration]: 
  StaticInjectorError(Platform: core)[StoreRouterConnectingModule -> InjectionToken @ngrx/router-store Configuration]: 
    NullInjectorError: No provider for InjectionToken @ngrx/router-store Configuration!
    at NullInjector.push../node_modules/@angular/core/fesm5/core.js.NullInjector.get (http://localhost:4200/vendor.js:44694:25)
    at resolveToken (http://localhost:4200/vendor.js:44932:24)
    at tryResolveToken (http://localhost:4200/vendor.js:44876:16)
    at StaticInjector.push../node_modules/@angular/core/fesm5/core.js.StaticInjector.get (http://localhost:4200/vendor.js:44780:20)
    at resolveToken (http://localhost:4200/vendor.js:44932:24)
    at tryResolveToken (http://localhost:4200/vendor.js:44876:16)
    at StaticInjector.push../node_modules/@angular/core/fesm5/core.js.StaticInjector.get (http://localhost:4200/vendor.js:44780:20)
    at resolveNgModuleDep (http://localhost:4200/vendor.js:61915:29)
    at _createClass (http://localhost:4200/vendor.js:61968:32)
    at _createProviderInstance (http://localhost:4200/vendor.js:61932:26)

2 个答案:

答案 0 :(得分:5)

经过一段时间的谷歌搜索(没有找到直接答案)后,我重新阅读了ngrx upgrade guide,它具有这个块:

  

StoreRouterConnectingModule现在需要使用forRoot

之前:

@NgModule({
  imports: [
    StoreRouterConnectingModule
  ]
})
export class AppModule {}

之后

@NgModule({
  imports: [
    StoreRouterConnectingModule.forRoot() // <-- gotta do this
  ]
})
export class AppModule {}

我在AppModule中修复了该问题,然后成功了!

自我说明:下一次阅读迁移指南的所有部分。

答案 1 :(得分:-1)

@Nathan Bedford解决方案起作用。