---
description:
globs:
alwaysApply: true
---
# Vue Vben Admin 开发规范与模板
## 1. 目录结构规范
```
views/
├── [module]/ # 模块目录
│ ├── data.ts # 表单schema、表格列配置等
│ ├── list.vue # 主列表页面
│ └── modules/ # 子模块组件
│ └── form.vue # 表单抽屉组件
```
## 2. 代码模板
### 2.1 列表页面模板 (list.vue)
```vue
```
### 2.2 数据配置模板 (data.ts)
```typescript
import type { VbenFormSchema } from '#/adapter/form';
import type { OnActionClickFn, VxeTableGridOptions } from '#/adapter/vxe-table';
import type { [Module]Api } from '#/api';
import { $t } from '#/locales';
// 表单配置
export function useFormSchema(): VbenFormSchema[] {
return [
{
component: 'Input',
fieldName: 'field_name',
label: $t('module.field.label'),
rules: 'required',
},
// ... 其他表单项
];
}
// 搜索表单配置
export function useGridFormSchema(): VbenFormSchema[] {
return [
{
component: 'Input',
fieldName: 'search_field',
label: $t('module.search.label'),
},
// ... 其他搜索项
];
}
// 表格列配置
export function useColumns(
onActionClick: OnActionClickFn,
onStatusChange?: (newStatus: any, row: T) => PromiseLike,
): VxeTableGridOptions['columns'] {
return [
{
field: 'field_name',
title: $t('module.field.label'),
width: 200,
},
// ... 其他列
{
align: 'center',
cellRender: {
attrs: {
nameField: 'name_field',
nameTitle: $t('module.name.label'),
onClick: onActionClick,
},
name: 'CellOperation',
},
field: 'operation',
fixed: 'right',
title: $t('module.operation'),
width: 130,
},
];
}
```
### 2.3 表单组件模板 (form.vue)
```vue
```
### 2.4 API 接口定义模板
```typescript
import type { BaseApi } from '#/api';
export namespace [Module]Api {
export interface [Module]Item {
id: number;
// ... 其他字段
}
export interface [Module]ListParams extends BaseApi.ListParams {
// ... 其他查询参数
}
export interface [Module]ListResult extends BaseApi.ListResult<[Module]Item> {}
}
export function get[Module]List(params: [Module]Api.[Module]ListParams) {
return request.get<[Module]Api.[Module]ListResult>('/api/[module]/list', { params });
}
export function create[Module](mdc:data: Omit<[Module]Api.[Module]Item, 'id'>) {
return request.post<[Module]Api.[Module]Item>('/api/[module]/create', data);
}
export function update[Module](mdc:id: number, data: Partial<[Module]Api.[Module]Item>) {
return request.put<[Module]Api.[Module]Item>(`/api/[module]/update/${id}`, data);
}
export function delete[Module](mdc:id: number) {
return request.delete(`/api/[module]/delete/${id}`);
}
```
### 2.5 国际化文案模板
```typescript
export default {
module: {
name: '[模块名称]',
list: '[模块名称]列表',
field: {
label: '[字段标签]',
},
search: {
label: '[搜索标签]',
},
operation: '操作',
},
};
```
## 3. 使用说明
1. 创建新模块时,按照目录结构规范创建相应的文件
2. 复制对应的模板代码到相应文件中
3. 替换所有 `[Module]` 为你的模块名称(如 `Product`)
4. 根据实际需求修改:
- 表单字段配置
- 搜索表单配置
- 表格列配置
- API 接口定义
- 国际化文案
## 4. 注意事项
1. 确保 API 接口已定义并正确实现
2. 确保国际化文案已配置
3. 根据实际需求调整表单和表格配置
4. 注意处理特殊字段(如时间、状态等)
5. 保持代码风格统一
6. 遵循项目的命名规范
## 5. 参考示例
可以参考以下现有模块的实现:
- @system/role
- @system/user
- @order