Angular 使用整数从字符串枚举中获取值

发布于 2025-01-19 18:32:15 字数 1049 浏览 4 评论 0原文

export enum RoleTypesEnum {
    RoleA = 'Role is A',
    RoleB = 'Role is B',
}

// in TS file
public RoleTypesEnum = RoleTypesEnum;

我想使用整数从枚举中获取字符串值(例如 角色是 B)。

如果我输入 HTML 或 TS 文件 console.log(RoleTypesEnum[0]) 它打印未定义

因为我从后端接收整数(例如:JSON -> RoleTypes:0 或 1)。

我可以创建一个管道并使用 Object.values(RoleTypesEnum); 获取枚举值,但我想知道最佳实践。管道解决方案:Angular Pipe 从枚举值中提取,但对任何枚举都是通用的

后端-前端同步性

数据库将枚举存储为整数。在后端(ASP.NET)中,DTO 和模型具有枚举类型的字段。当 Controller 返回时,它会自动从枚举返回一个 JSON 格式的整数。

  1. 如果从后端我返回字符串值(json:“roleB”)而不是整数(json:1)。在返回 JSON 之前,我需要从 Enum 转换为字符串。我想破坏数据库和后端设计。
  2. 如果我从后端返回整数当前问题案例),则我无法拥有自定义字符串值。因为我唯一的数据是一个整数,并且 'RoleTypesEnum[1]' =>不明确的。 除非我用整数设置枚举
export enum RoleTypesEnum {
    RoleA = 0,
    RoleB = 1,
}

,但是我不能有自定义字符串值(例如:“角色是 B”)。

export enum RoleTypesEnum {
    RoleA = 'Role is A',
    RoleB = 'Role is B',
}

// in TS file
public RoleTypesEnum = RoleTypesEnum;

I want to get string value (ex Role is B) from enum using an integer.

If I type in HTML or TS file
console.log(RoleTypesEnum[0])
it prints undefined.

Because I receive integer from backend (ex: JSON -> RoleTypes: 0 or 1).

I can make a pipe and get enum values using Object.values(RoleTypesEnum);, but I am wondering for the best practice. The pipe solution: Angular Pipe extracts from Enum values, but generic for any Enum

Backend-Frontend synchronicity

The database stores the enum as integers. In backend (ASP.NET), DTO and models have field with enum type. When Controller returns, it automatically returns an integer in JSON from the enum.

  1. If from backend I return string value (json: "roleB") instead of integer (json: 1). I need a conversion from Enum to string before returning the JSON. And I feel like wrecking with the DB and backend design.
  2. If from backend I return integer (current question case), I can't have custom string values. Because the only data I have is an integer and 'RoleTypesEnum[1]' => undefined.
    Unless I set the enum with integers
export enum RoleTypesEnum {
    RoleA = 0,
    RoleB = 1,
}

But then I can't have custom string values (ex: "Role is B").

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

╰沐子 2025-01-26 18:32:15

设法以这种方式具有自定义字符串:

共享/enums-deScriptions.ts

export const MyTypesEnumDescription: {
    [key in MyTypesEnum]: string;
} = {
    [MyTypesEnum.Standard]: 'Standard',
    [MyTypesEnum.Advanced]: 'Advanced Stage',
    [MyTypesEnum.Intermediate]: 'Intermediate Stage',
};

myclass.ts

public selectBoxItemList = [
    new SelectListItem({
        value: MyTypesEnum.Advanced,
        text: MyTypesEnumDescription.InvalidCall,
    }),     

Managed to have custom strings in this way:

Shared/enums-descriptions.ts

export const MyTypesEnumDescription: {
    [key in MyTypesEnum]: string;
} = {
    [MyTypesEnum.Standard]: 'Standard',
    [MyTypesEnum.Advanced]: 'Advanced Stage',
    [MyTypesEnum.Intermediate]: 'Intermediate Stage',
};

MyClass.ts

public selectBoxItemList = [
    new SelectListItem({
        value: MyTypesEnum.Advanced,
        text: MyTypesEnumDescription.InvalidCall,
    }),     
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文