【问题标题】:typescript does not recognize exported enum打字稿无法识别导出的枚举
【发布时间】:2018-06-03 19:21:26
【问题描述】:

我正在使用TypeScript 2.5.3Angular 5
在单独的文件中声明了一个enum,如下所示:

export enum eUserType {
  Driver = 1,
  Passenger = 2,
  User = 3
}

然后像这样在其他ts 文件中导入/使用它:

import { eUserType } from '../CorrectFilePath/eUserType';
export class ViewsModule { 
  newVariable=eUserType.Driver;
}

虽然智能感知工作正常,但运行时会引发错误:Cannot read property 'Driver' of undefined
我是在滥用enum 还是什么?

更新: 我不知道这是否相关。但是,目标模块是延迟加载的,并且在那里使用了PreloadingStrategy 类(在模块文件以外的文件中声明),而我的enum 应用于PreloadingStrategy 类。

【问题讨论】:

  • 您的代码stackblitz.com/edit/angular-enum没有任何运行时错误@
  • 可以分享一下ViewsModule使用的代码吗?
  • 目标模块是延迟加载的,并在那里使用了PreloadingStrategy 类(在模块文件以外的文件中声明),我的enum 应用于PreloadingStrategy类。
  • 它是否适用于export const enum eUserType {...}
  • @Alex Szabó Yeeesssss。但是为什么???

标签: angular typescript enums


【解决方案1】:

这可能无法帮助您解决当前的问题(您的设置似乎很好),而且我对延迟加载还不够熟悉,无法判断,但是您使用 1, 2, 3 作为枚举值(可以省略),并且export const enum 不会创建对象并尝试引用它,但实际上将值放在您将使用它们的位置。我认为对于您的用例,这将是一个完美的选择:http://www.typescriptlang.org/docs/handbook/enums.html#const-enums

【讨论】:

    【解决方案2】:

    试试这个,

    euserType = eUserType;
    newVariable=euserType.Driver;
    

    【讨论】:

      猜你喜欢
      • 2017-03-09
      • 2020-04-30
      • 2023-03-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-02-12
      • 2017-05-09
      相关资源
      最近更新 更多