从 Angular 16 升级到 Angular 17 后,将 Angular 构建器更改为使用 esbuild 构建器:
@angular-devkit/build-angular:browser
-> @angular-devkit/build-angular:application
。当我提供应用程序时,我遇到了这个运行时错误:
Error: NG0203: inject() must be called from an injection context such as a constructor, a factory function, a field initializer, or a function used with runInInjectionContext. Find more at ``https://angular.io/errors/NG0203`` at injectInjectorOnly (core.mjs:1093:15) at ɵɵinject2 (core.mjs:1106:42) at Object.FeatureEffects_Factory [as factory] (feature-effects.ts:15:28) at chunk-BKMMLQFQ.js?v=f9269614:1898:35 at runInInjectorProfilerContext (chunk-BKMMLQFQ.js?v=f9269614:523:5) at R3Injector.hydrate (chunk-BKMMLQFQ.js?v=f9269614:1897:11) at R3Injector.get (chunk-BKMMLQFQ.js?v=f9269614:1790:23) at injectInjectorOnly (chunk-BKMMLQFQ.js?v=f9269614:646:36) at ɵɵinject (chunk-BKMMLQFQ.js?v=f9269614:652:59) at inject (chunk-BKMMLQFQ.js?v=f9269614:661:10)
app.module.ts
@NgModule({
declarations: [AppComponent],
imports: [
BrowserModule,
StoreModule.forRoot(reducers, {
metaReducers,
runtimeChecks: {
strictStateImmutability: false,
strictActionImmutability: false
}
}),
EffectsModule.forRoot([FeatureEffects])
],
providers: [],
bootstrap: [AppComponent],
exports: []
})
export class AppModule {}
Ngrx:
“@ngrx/effects”:“17.2.0”,
“@ngrx/实体”:“17.2.0”,
“@ngrx/路由器商店”:“17.2.0”,
“@ngrx/store”:“17.2.0”,
“@ngrx/store-devtools”:“17.2.0”
角度版本:17
节点:v18.19.0
Stackblitz 示例:https://stackblitz.com/~/github.com/thoussem/ngrx17-esbuild-issue
根据 Angular 文档,从 @angular-devkit/build-angular:browser 迁移到 @angular-devkit/build-angular:application 时没有重大更改,因此一切都应该正常工作
我在使用 Angular 17 和 nx 19 的 nx monorepo 上遇到了同样的问题
要修复它,我必须更新 tsconfig.base.json,并删除
paths: { "@angular/*": ["./node_modules/@angular"] }
。
这是一个来自角度的错误,尽管这条路径应该有所帮助。但这会产生错误。
其次,在你的 angular.json 或(project.json 如果使用 Nx)
在构建选项中添加 "preserveSymlinks": true
。
希望这对您有帮助!