Skip to content

字典管理

功能特性

  • 字典 CRUD(类型 + 字典项,树形结构)
  • 前端 useDict 组合式函数联动业务表单
  • 内置缓存(重复请求不重复调用)
  • 预置字典:user_statusmenu_typegenderyes_no

后端接口

方法路径说明
GET/dictionaries字典列表
GET/dictionaries/code/{code}按编码获取字典项
POST/dictionaries创建
PUT/dictionaries/{id}更新
DELETE/dictionaries/{id}删除

前端使用

使用 useDict 组合式函数从后端字典接口加载选项:

ts
const { options, loading } = useDict('user_status');
// options → [{ label: '启用', value: '1' }, { label: '禁用', value: '0' }]

在表单 schema 中动态注入:

ts
const formSchema: VbenFormSchema[] = [
  { component: 'Select', fieldName: 'status', label: '状态',
    componentProps: { options: [] } },  // 初始为空
];

// 弹窗打开时动态更新
formApi.updateSchema([
  { fieldName: 'status', componentProps: { options: options.value } },
]);

useDict 内置缓存,重复调用同字典不会重复请求。

预置字典

字典编码说明
user_status用户状态
menu_type菜单类型
gender性别
yes_no是否

基于 MIT 协议发布