【发布时间】:2020-10-07 07:20:00
【问题描述】:
我喜欢将 json 字符串值转换为枚举,以便它可以在 html 页面上显示/呈现自定义字符串。 错误信息:输入'{ id: number;名称:字符串;状态:字符串; }[]' 不能分配给类型 'Status[]'
我有一个这样的 json 记录:
{ id: 1, name: 'Some name', status: 'STATUS01' },
status.enum.ts
export enum Status {
'STATUS01' = 'Operational',
'STATUS02' = 'Some other status'
}
该枚举在模型中使用
import { Status } from './status.enum';
export class ServiceState {
id: number;
name: string;
status: Status;
}
在服务中有一个检索所有状态(虚拟数据)的功能:
getStatuses(): Observable<ServiceState[]> {
const response = [
{ id: 1, name: 'One', status: 'STATUS01' },
{ id: 2, name: 'Two', status: 'STATUS01' },
{ id: 3, name: 'OneTwo', status: 'STATUS02' },}
];
return of(response);
}
返回是抛出错误
【问题讨论】:
-
请添加引发错误的代码。
标签: json angular typescript serialization enums