Angular中可以通过以下步骤从字符串值中获取枚举键:
enum Color {
Red = 'red',
Blue = 'blue',
Green = 'green'
}
getEnumKey(value: string): string {
return Color[value as keyof typeof Color];
}
const enumKey = this.getEnumKey('blue');
console.log(enumKey); // 输出 'Blue'
这样,我们就可以通过字符串值获取到对应的枚举键。
关于Angular的更多信息,你可以参考腾讯云的产品介绍链接:Angular - 腾讯云
领取专属 10元无门槛券
手把手带您无忧上云