# 页面视图开发 :::tip 说明 页面视图位于 `src/views/` 目录,每个业务模块遵循 `index.vue`(列表页)+ `edit.vue`(编辑弹窗)的标准模式,辅以 `columns.ts`(列定义)和 `querySchemas.ts`(搜索表单)配置文件。 ::: ## 目录结构 ``` src/views/ ├── system/ │ ├── position/ │ │ ├── index.vue # 岗位列表页 │ │ ├── edit.vue # 岗位编辑弹窗 │ │ ├── columns.ts # 表格列定义 │ │ └── querySchemas.ts # 搜索表单定义 │ ├── user/ │ │ ├── index.vue │ │ └── edit.vue │ ├── role/ │ │ ├── index.vue │ │ └── edit.vue │ └── ... ├── cms/ │ ├── article/ │ └── ... └── ... ``` ## 文件职责 | 文件 | 职责 | 说明 | |------|------|------| | `index.vue` | 列表页 | 搜索表单 + 工具栏 + BasicTable + 编辑弹窗引用 | | `edit.vue` | 编辑弹窗 | basicModal + n-form,懒加载 | | `columns.ts` | 表格列定义 | 定义列 key、title、render 函数等 | | `querySchemas.ts` | 搜索表单定义 | FormSchema 数组,定义搜索字段 | ## index.vue 列表页 以岗位管理为例,完整结构如下: ```vue ``` ## edit.vue 编辑弹窗 ```vue ``` ## 数据流 ``` 页面加载 → BasicForm 注册 → BasicTable request 触发 → loadDataTable → API → 渲染表格 用户搜索 → handleSubmit → 更新 formParams → reloadTable → 重新请求 用户新增 → handleAdd → positionId=0 → editVisible=true → edit.vue 加载 → 表单填写 → 提交 用户编辑 → handleEdit → positionId=id → editVisible=true → edit.vue 加载详情 → 修改 → 提交 用户删除 → dialog.warning → API → reloadTable ``` ## columns.ts ```typescript // src/views/system/position/columns.ts import { h } from 'vue'; import { NTag } from 'naive-ui'; export const columns = [ { type: 'selection', width: 50, fixed: 'left', }, { title: 'ID', key: 'id', fixed: 'left', width: 50, }, { title: '岗位名称', key: 'name', width: 100, }, { title: '岗位状态', key: 'status', width: 100, render(record) { return h( NTag, { style: { marginRight: '6px' }, bordered: false, type: record.status == 1 ? 'success' : 'error', }, { default: () => (record.status == 1 ? '正常' : '停用') }, ); }, }, { title: '排序', key: 'sort', width: 100, }, { title: '创建人', key: 'createUser', width: 100, }, { title: '创建时间', key: 'createTime', width: 180, }, ]; ``` ## querySchemas.ts ```typescript // src/views/system/position/querySchemas.ts import { FormSchema } from '@/components/Form/index'; export const schemas: FormSchema[] = [ { field: 'name', component: 'NInput', label: '岗位名称', componentProps: { placeholder: '请输入岗位名称', }, }, { field: 'status', component: 'NSelect', label: '状态', componentProps: { placeholder: '请选择状态', clearable: true, options: [ { label: '正常', value: '1' }, { label: '禁用', value: '2' }, ], }, }, ]; ``` :::tip 温馨提示 `columns.ts` 和 `querySchemas.ts` 独立为配置文件,便于维护和复用。`columns` 中的 `render` 函数使用 Vue 的 `h()` 创建虚拟 DOM,可实现任意复杂的自定义渲染。`render` 函数直接接收行数据对象,通过 `record.key` 访问字段值(如 `record.status`)。`querySchemas` 中的 `component` 使用 NaiveUI 组件名(如 `NInput`、`NSelect`),`options` 硬编码或通过字典 API 动态获取。 ::: ## 总结 页面视图遵循 `index.vue` + `edit.vue` + `columns.ts` + `querySchemas.ts` 标准模式。列表页使用 `BasicForm` 搜索 + `BasicTable` 展示数据 + `TableAction` 操作按钮,编辑弹窗使用 `defineAsyncComponent` 懒加载。数据流清晰:搜索更新参数 → 刷新表格,操作成功 → emit 事件 → 刷新表格。消息提示使用 `useMessage()`,确认对话框使用 `useDialog()`,图标来自 `@vicons/antd`。