2016-07-24 75 views
10

我创建了一个枚举,但我无法导入和使用VS15中的枚举。如何导入枚举

这是包含在enums.ts枚举:

enum EntityStatus { 
    New = 0, 
    Active = 1, 
    Archived = 2, 
    Trashed = 3, 
    Deleted = 4 
} 

Visual Studio中看到此枚举,甚至没有进口,因此不会给一个编译时错误。但在运行时,则会引发错误

Cannot read property 'Archived' of undefined. 

所以现在我尝试导入它像我导入其他类:

import {EntityStatus} from "../../core/enums"; 

的Visual Studio现在给一个编译时错误:

"...enums is not a module ..." 

那么如何导入枚举呢?

回答

15

我错过了出口关键字:

export enum EntityStatus { 
     New = 0, 
     Active = 1, 
     Archived = 2, 
     Trashed = 3, 
     Deleted = 4 
} 

然后它的工作如预期。

10

请试试这个。它为我

enums.ts

export enum Category {Cricket,Tennis,Golf,Badminton} 

和喜欢的方式所需要的.ts文件导入如下:

import {Category} from './enums' 
4

你会得到相同的Cannot read property 'Foo' of undefined.运行时,当你发生错误在其中一个TypeScript声明文件(*.d.ts)中定义您的Enum,因为这些文件不会转化为JavaScript。

用示例应用程序的更多细节可以发现here

+0

我不认为a * .d.ts会是一个定义Enum的好地方。 –

+1

嗯,我也是。特别是在弄清楚为什么我的枚举没有定义之后浪费时间:) – luk355