Angular No provider for EffectsRootModule错误消息的出现原因和修复方式

123 阅读1分钟

错误消息:

main.ts:12 NullInjectorError: R3InjectorError(AppModule)[EffectsFeatureModule -> EffectsRootModule -> EffectsRootModule -> EffectsRootModule]:
NullInjectorError: No provider for EffectsRootModule!
at NullInjector.get (http://localhost:4200/vendor.js:62758:27)
at R3Injector.get (http://localhost:4200/vendor.js:76765:33)
at R3Injector.get (http://localhost:4200/vendor.js:76765:33)
at R3Injector.get (http://localhost:4200/vendor.js:76765:33)
at injectInjectorOnly (http://localhost:4200/vendor.js:62613:33)
at Module.ɵɵinject (http://localhost:4200/vendor.js:62623:57)
at Object.EffectsFeatureModule_Factory [as factory] (http://localhost:4200/vendor.js:137576:239)
at R3Injector.hydrate (http://localhost:4200/vendor.js:77003:63)
at R3Injector.get (http://localhost:4200/vendor.js:76753:33)
at http://localhost:4200/vendor.js:76799:25
(anonymous) @ main.ts:12
invoke @ zone-evergreen.js:364
run @ zone-evergreen.js:123
(anonymous) @ zone-evergreen.js:857
invokeTask @ zone-evergreen.js:399
runTask @ zone-evergreen.js:167
drainMicroTaskQueue @ zone-evergreen.js:569
Promise.then (async)
scheduleMicroTask @ zone-evergreen.js:552
scheduleTask @ zone-evergreen.js:388
scheduleTask @ zone-evergreen.js:210
scheduleMicroTask @ zone-evergreen.js:230
scheduleResolveOrReject @ zone-evergreen.js:847
then @ zone-evergreen.js:979
bootstrapModule @ core.js:42679
./src/main.ts @ main.ts:11
webpack_require @ bootstrap:79
0 @ unless.directive.ts:33
webpack_require @ bootstrap:79
checkDeferredModules @ bootstrap:45
webpackJsonpCallback @ bootstrap:32
(anonymous) @ main.js:1

应用程序自己开发的module:


EffectsFeatureModule是dependent type之一:


value是null,所以报错:

在app.module.ts里加上一行语句即可解决问题:

EffectsModule.forRoot([AppEffects]),

这个forRoot方法为EffectsRootModule提供了可用的provider:

要获取更多Jerry的原创文章,请关注公众号"汪子熙":