enum.js
/**
* 获取枚举值:STATUSMAP.TTT
* 获取枚举描述:STATUSMAP.getDesc('SH')
* 通过枚举值获取描述:STATUSMAP.getDescFromValue('TG')
*/
function createEnum(definition) {
const valueMap = {};
const descMap = {};
for (const key of Object.keys(definition)) {
const [value, desc] = definition[key];
valueMap[key] = value;
descMap[value] = desc;
}
return {
...valueMap,
getDesc(key) {
return (definition[key] && definition[key][1]) || '无';
},
getDescFromValue(value) {
return descMap[value] || '无';
}
}
}
// 投票状态
const VOTE_STATUS = createEnum({
0: ["WFB", '未发布'],
1: ["YFB", '已发布'],
2: ["TPJS", '投票结束'],
});
// 删除状态
const DELETE_STATUS = createEnum({
0: ["WSC", "未删除"],
1: ["YSC", "已删除"],
})
// 投票类型
const VOTE_TYPE = createEnum({
0 : ["YCX", "一次性"],
1 : ["XSZQ", "小时周期"],
2 : ["TZQ", "天周期"],
})
export {VOTE_STATUS, DELETE_STATUS, VOTE_TYPE}
组件中
import {VOTE_STATUS, DELETE_STATUS, VOTE_TYPE} from "@/utils/enum";
console.log(VOTE_STATUS.getDesc(0)); // 未发布
标签:STATUS,definition,const,enum,value,js,key,VOTE,字典
From: https://www.cnblogs.com/openmind-ink/p/17124682.html