我使用的是TypeScript 2.5.3
和Angular 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
类。
发布于 2018-06-10 02:29:21
这可能不会帮助您解决当前的问题(您的设置似乎很好),而且我对延迟加载还不够熟悉,因此无法判断,但是您正在使用1, 2, 3
作为枚举值(可以省略),并且export const enum
不会创建对象并尝试引用它,而是将值放在您应该使用它们的地方。我认为对于您的用例,这将是一个完美的选择:http://www.typescriptlang.org/docs/handbook/enums.html#const-enums
发布于 2018-06-04 03:25:40
尝尝这个,
euserType = eUserType;
newVariable=euserType.Driver;
https://stackoverflow.com/questions/50670145
复制相似问题