快速上手
CRUD 是配置驱动的增删改查套件:声明搜索字段、表格列和表单字段,组件处理查询联动、分页、权限按钮、删除确认和保存后刷新。
组件总览
| 组件 | 职责 | 文档 |
|---|---|---|
| CrudPage | 组合搜索、表格和表单,标准页面的首选 | CrudPage |
| CrudSearch | 搜索表单 | CrudSearch |
| CrudTable | 数据表格、工具栏、导入导出 | CrudTable |
| CrudForm | 新增、编辑、查看共用弹窗表单 | CrudForm |
组件已自动注册,模板中直接使用;配置类型从 @/components/Crud/types 导入。需要自定义页面结构时,参见分组件组合。
完整示例
以用户管理为例:
vue
<template>
<CrudPage
:search-config="searchConfig"
:table-config="tableConfig"
:form-config="formConfig"
:load-form-data="(row) => UserAPI.getFormData(String(row.id))"
>
<template #table-cell-status="{ row }">
<el-tag :type="row.status === 1 ? 'success' : 'info'">
{{ row.status === 1 ? "启用" : "禁用" }}
</el-tag>
</template>
</CrudPage>
</template>
<script setup lang="ts">
import RoleAPI from "@/api/system/role";
import UserAPI from "@/api/system/user";
import type { UserForm, UserItem, UserQuery } from "@/api/system/user";
import type { CrudFormConfig, CrudSearchConfig, CrudTableConfig } from "@/components/Crud/types";
const searchConfig: CrudSearchConfig = {
fields: [
{ prop: "keywords", label: "关键词", type: "input", attrs: { clearable: true } },
{
prop: "status",
label: "状态",
type: "select",
initialValue: null,
options: [
{ label: "启用", value: 1 },
{ label: "禁用", value: 0 },
],
attrs: { clearable: true },
},
],
};
const tableConfig: CrudTableConfig<UserQuery, UserItem> = {
permissionPrefix: "sys:user",
fetchData: UserAPI.getPage,
deleteByIds: UserAPI.deleteByIds,
toolbarActions: ["create", "delete"],
tableTools: ["refresh", "columns", "search"],
columns: [
{ type: "selection", width: 50 },
{ prop: "username", label: "用户名", hideable: false },
{ prop: "nickname", label: "昵称" },
{ prop: "roleNames", label: "角色" },
{ prop: "status", label: "状态", width: 90 },
{ prop: "createTime", label: "创建时间", width: 180, sortable: "custom" },
{
prop: "actions",
label: "操作",
cellType: "actions",
width: 200,
fixed: "right",
actions: ["view", "edit", "delete"],
},
],
};
const formConfig: CrudFormConfig<UserForm> = {
title: ({ mode }) => ({ create: "新增用户", edit: "编辑用户", view: "查看用户" })[mode],
fields: [
{
prop: "username",
label: "用户名",
type: "input",
required: true,
readonly: ({ mode }) => mode !== "create",
},
{ prop: "nickname", label: "昵称", type: "input", required: true },
{
prop: "roleIds",
label: "角色",
type: "select",
initialValue: [],
attrs: { multiple: true },
rules: [{ type: "array", required: true, min: 1, message: "请选择角色", trigger: "change" }],
async onInit(field) {
field.options = await RoleAPI.getOptions();
},
},
{
prop: "status",
label: "状态",
type: "switch",
initialValue: 1,
attrs: { activeValue: 1, inactiveValue: 0 },
},
],
submitForm(data, { mode, id }) {
if (mode === "create") return UserAPI.create(data);
if (id === undefined) throw new Error("缺少用户 ID");
return UserAPI.update(String(id), data);
},
};
</script>项目内完整实现见 src/views/demo/crud/index.vue,包含字典插槽、表头筛选、导入导出和自定义行操作。
内置行为
- 搜索、重置、翻页、表头筛选自动联动查询,未提交的输入不影响刷新和导出
- 内置按钮按
permissionPrefix校验权限,无权限不渲染 - 删除自带确认弹窗,支持单条和批量,成功后刷新
- 表单保存成功自动关闭并刷新列表;编辑、查看通过
loadFormData加载详情 - 列表请求竞态隔离,仅最新响应回填
更新接口需要但表单不展示的字段
在 fields 中声明 hidden: true 保留原值,详见 CrudForm。
