vue3 declares the field name enum type
Below is the enumeration declaration of Type, with a total of 6 fields
enum Type { primary = "primary", success = "success", warning = "warning", warn = "warn", // warning alias danger = "danger", info = "info", }
There are two keywords that declare types in TypeScript, interface and type, which are slightly different when declaring fields of key with uncertain types.
Use type to declare:
type ColorConfig = { [key in Type]: Colors; };
Using interface can only be like the following:
interface ColorConfig { [key: string]: Colors; }
Because the index of interface can only be the basic type, and type alias cannot be used either. The index of type can be a composite type.
Vue usage enhancement "enumeration" application
Method 1 (suitable for simple filters)
// **document/** * Get the enum value: * Get enumeration description: ('SH') * Get the description by enumeration value: ('TG') */ let STATUSMAP = createEnum({ SH: ['SH', 'Additional'], TG: ['TG', 'Audit passed'] }); function createEnum(definition) { const valueMap = {}; const descMap = {}; for (const key of (definition)) { const [value, desc] = definition[key]; valueMap[key] = value; descMap[value] = desc; } return { ...valueMap, getDesc(key) { return (definition[key] && definition[key][1]) || 'none'; }, getDescFromValue(value) { return descMap[value] || 'none'; } } } export default STATUSMAP;
view file
<el-row> <el-button>Enumeration tests</el-button> <p>Current status:{{('SH')}}</p> <p>You can also obtain the description by enumeration name:{{('HHH')}}</p> </el-row> <!-- Used in filters ThenfiltersUse functions directly in the filter to return the value -->
Fang Shi Er (filter, loop list)
// document/** * Define enumeration value */ export default { order: [ { value: 'TJ', label: 'Committed' }, { value: 'CZ', label: 'Processing' }, { value: 'CL', label: 'Processed' }, ], orderDetail: [ { value: 'DF', label: 'Awaiting shipment' }, { value: 'FH', label: 'Shipped' }, { value: 'QS', label: 'Signed' }, ] }
// document/** * Define the enumeration tool * */ import order from './order/'; let constants = { ...order }; let valueMap = {}; let nameMap = {}; (constants).forEach(key => { valueMap[key] = []; nameMap[key] = {}; constants[key].forEach(event => { valueMap[key].push(event); nameMap[key][] = ; }); }); export { valueMap, nameMap }
/** * view file */ <template> <h3>Enumeration values are used for display</h3> <el-row> <el-button v-for="(item, index) in " :key="index">{{}}</el-button> </el-row> <h3>Enumeration value filter</h3> <el-row> <el-button>{{enumValue | filterStatus('orderDetail')}}</el-button> </el-row> </template> <script> import { valueMap, nameMap } from '@/constants'; export default { data() { return { STATUS: STATUS, valueMap, enumValue: 'FH', // Delivery } }, filters:{ filterStatus: function(val, key){ if(!val && val !== 0){ return 'none'; } return nameMap[key][val]; } } }
The above is personal experience. I hope you can give you a reference and I hope you can support me more.