You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Angular 15升级至16后Enum相关初始化错误求助

Angular 15升级至16后枚举引发模块初始化失败问题

问题现象

将Angular从15版本升级到16版本后,部分页面完全无法加载,报错信息如下:

Uncaught Error: Uncaught (in promise): ReferenceError: Cannot access 'HomeModule' before initialization
ReferenceError: Cannot access 'HomeModule' before initialization
at Module.HomeModule(src_app_components_signed-in_pages_promotions_home_home_module_ts.js:12:62)
at app.routing.ts:303:102

追踪后确认:所有使用枚举的模块和组件都会触发该问题。

相关代码细节

枚举定义(PurchaseType.ts)

export enum PurchaseType {
  Bulk = 1,
  Single = 2
}

枚举导出方式

所有枚举通过单个文件统一导出:

export * from './objects/orderingSystem/purchasing-type/PurchaseType';

路由配置(app-routing.ts)

{
  path: 'purchase-type',
  loadChildren: () =>
    import('./components/signed-in/pages/customerPurchase/mainPurchase/mainPurchase.module').then((m) => m.MainPurchaseModule),
}

模块中使用枚举

import { PurchaseType } from '@abc/orderingSystem';
@NgModule({
  imports: [
    ABCModule.forRoot(),
    BCDModule.forRoot(),
    RouterModule.forChild([
      {
        path: 'purchased-items',
        component: PurchasedPage,
        data: { bulk: PurchaseType.Bulk, single: PurchaseType.Single},
      },
   ]),
  ],
})

已尝试的无效解决方案

  • 修改枚举导入路径:从import { PurchaseType } from '@abc/orderingSystem';改为import { PurchaseType } from 'dataTransferObjects/objects/purchasing-type/PurchaseType';,路由临时恢复正常,但服务器重启后错误重现。
  • 将枚举改为const enum:
    export const enum PurchaseType {
      Bulk = 1,
      Single = 2
    }
    
  • 将枚举改为字符串值类型:
    export enum PurchaseType {
      Bulk = '1',
      Single = '2'
    }
    

以上三种方式均无法彻底解决问题。

相关配置文件(tsconfig.json)

{
  "compileOnSave": false,
  "compilerOptions": {
    "baseUrl": "./",
    "outDir": "../abcServer/wwwroot/dist",
    "skipLibCheck": true,
    "sourceMap": true,
    "declaration": false,
    "downlevelIteration": true,
    "experimentalDecorators": true,
    "module": "esnext",
    "moduleResolution": "node",
    "importHelpers": true,
    "target": "es2015",
    "typeRoots": [ "node_modules/@types" ],
    "lib": [ "es2018", "dom" ],
    "paths": {
      "@abc/services/*": [ "src/app/services/*" ],
      "@abc/functions/*": [ "src/app/functions/*" ],
      "@abc/classes/*": [ "src/app/classes/*" ],
      "@abc/api/*": [ "src/app/dataServices/*" ],
      "@abc/analytics": [ "dataTransferObjects/analytics" ],
      "@abc/category": [ "dataTransferObjects/category" ],
      "@abc/customer": [ "dataTransferObjects/customer" ],
      "@abc/orderingSystem": [  "dataTransferObjects/orderingSystem"]
    }
  },
  "angularCompilerOptions": {
    "fullTemplateTypeCheck": true,
    "strictInjectionParameters": true,
  }
}

疑问

升级Angular 16时是否遗漏了关键配置?是否有其他人遇到过类似问题?恳请提供解决思路。

内容的提问来源于stack exchange,提问作者LeoDiCap235

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.19 07:23:13