[英]How to get values of enum in typescript
I have a problem with getting values from enum. 我在从枚举中获取值时遇到问题。 Here is the snippet of my code:
这是我的代码片段:
export const enum ComplianceType {
ENGINEER_ASSESMENT = 'ENGINEER_ASSESMENT',
CONSTRUCTION_COMPLIANCE = 'CONSTRUCTION_COMPLIANCE',
ARCHITECTURE_ASSIGNMENT = 'ARCHITECTURE_ASSIGNMENT',
BLUEPRINT_APPROVAL = 'BLUEPRINT_APPROVAL',
THERMAL_COMPLIANCE = 'THERMAL_COMPLIANCE',
ELECTRICITY_COMPLIANCE = 'ELECTRICITY_COMPLIANCE',
TELECOMUNICATION_COMPLIANCE = 'TELECOMUNICATION_COMPLIANCE',
WATER_COMPLIANCE = 'WATER_COMPLIANCE',
OTHER_1 = 'OTHER_1',
OTHER_2 = 'OTHER_2',
}
in my .ts
file: 在我的
.ts
文件中:
import { ICompliance, ComplianceType } from 'app/shared/model/compliance.model';
And i tried this one: 我尝试了这个:
complianceTypeList: ComplianceType;
I don't get how to get values from enum in typescript. 我不知道如何从打字稿中的枚举中获取值。 Any advice ?
有什么建议吗?
Your syntax is wrong. 您的语法错误。
You should not assign it, or try to assign Enums as variables. 您不应分配它,或尝试将枚举分配为变量。 Just
export enum ComplianceType
will do. 只需
export enum ComplianceType
就可以。
export enum ComplianceType {
ENGINEER_ASSESMENT = 'ENGINEER_ASSESMENT',
CONSTRUCTION_COMPLIANCE = 'CONSTRUCTION_COMPLIANCE',
ARCHITECTURE_ASSIGNMENT = 'ARCHITECTURE_ASSIGNMENT',
BLUEPRINT_APPROVAL = 'BLUEPRINT_APPROVAL',
THERMAL_COMPLIANCE = 'THERMAL_COMPLIANCE',
ELECTRICITY_COMPLIANCE = 'ELECTRICITY_COMPLIANCE',
TELECOMUNICATION_COMPLIANCE = 'TELECOMUNICATION_COMPLIANCE',
WATER_COMPLIANCE = 'WATER_COMPLIANCE',
OTHER_1 = 'OTHER_1',
OTHER_2 = 'OTHER_2',
}
From there, you can simply import the Enums
on the Class, or component, that requires it. 从那里,您可以简单地在需要它的类或组件上导入
Enums
。
import { ComplianceType } from 'app/shared/model/compliance.model';
You can access an Enum value by treating it like an array and accessing its value by using the key: 您可以通过将Enum值视为数组并使用键来访问其值来访问Enum值:
ComplianceType["ARCHITECTURE_ASSIGNMENT"]
Should give you the value. 应该给你的价值。
You can use Object.keys(EnumType);
您可以使用
Object.keys(EnumType);
if you want to access the keys of your enums, or Object.values(EnumType);
如果要访问枚举的键,
Object.values(EnumType);
if you want to have access to its values. 如果您想访问其值。 Nevertheless, it looks like you are trying to assign an entire Enum to a single Enum property type here
但是,您似乎在这里尝试将整个Enum分配给单个Enum属性类型。
You can do this: 你可以这样做:
complianceType: ComplianceType;
complianceType = ComplianceType.ENGINEER_ASSESMENT;
But you can't assign a type to a variable of this type, basically. 但是,基本上,您不能将类型分配给该类型的变量。 That does not make sens.
那没有意义。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.