前端开发规范 (v3.7.3)
Vue 3.5 + Vite 8 + TypeScript 5.9 + pnpm
目录
开发命令
# 安装依赖
pnpm install
# 开发服务器
pnpm dev
# 生产构建
pnpm build
# 构建分析
pnpm build:report
# 类型检查
pnpm type-check
# 代码格式化
pnpm fmt
# 代码检查
pnpm lint
# 代码检查并修复
pnpm lint:fix
# 清除缓存
pnpm clean:cache代码风格检查
ESLint
ESLint 是用于 JavaScript 和 TypeScript 代码规范检查的工具。
手动校验
# 代码检查
pnpm lint
# 代码检查并自动修复
pnpm lint:fix配置
项目的 ESLint 配置文件位于根目录下的 eslint.config.js。
编辑器集成
推荐使用 VSCode 进行开发,项目已包含 ESLint 配置(位于 .vscode/settings.json)。
Prettier
Prettier 用于统一项目代码风格。
手动格式化
# 格式化代码
pnpm fmt
# 检查格式(不修改文件)
pnpm fmt:checkStylelint
Stylelint 用于 CSS/LESS 代码风格校验。
配置
配置文件位于根目录下的 stylelint.config.js。
TypeScript
类型检查
# 快速类型检查(跳过库检查)
pnpm type-check
# 完整类型检查
pnpm type-check:fullGit 提交规范
CommitLint
项目使用 CommitLint 进行 Git 提交信息规范校验。
提交规范
遵循 Conventional Commits 规范:
| 类型 | 说明 |
|---|---|
feat | 新增功能 |
fix | 修复问题/BUG |
style | 代码风格改变,不影响运行结果 |
perf | 性能优化 |
refactor | 代码重构 |
revert | 撤销修改 |
test | 测试相关 |
docs | 文档/注释 |
chore | 依赖更新/构建配置修改等 |
ci | 持续集成 |
workflow | 工作流改进 |
示例
git commit -m 'feat(home): add home page'
git commit -m 'fix(api): resolve token refresh issue'
git commit -m 'docs: update README'跳过检查
# 使用 --no-verify 跳过 Git hook 校验
git commit -m "xxx" --no-verify代码风格指南
Vue 3 组件规范
使用 <script setup>
<script setup lang="ts">
import { ref, computed } from 'vue';
// ✅ 推荐
const count = ref(0);
const doubled = computed(() => count.value * 2);
</script>组件命名
<!-- ✅ 推荐:多词组件名 -->
<UserProfile />
<SettingsPanel />
<!-- ❌ 避免:单词组件名 -->
<User />
<Settings />TypeScript 规范
类型定义
// ✅ 推荐:使用 interface 定义对象类型
interface User {
id: number;
name: string;
email?: string;
}
// ✅ 推荐:使用 type 定义联合类型
type Status = 'pending' | 'active' | 'inactive';
// ✅ 推荐:使用泛型
function fetchData<T>(url: string): Promise<T> {
// ...
}避免使用 any
// ❌ 避免
const data: any = response.data;
// ✅ 推荐:使用 unknown 或具体类型
const data: unknown = response.data;
const user: User = response.data as User;导入规范
路径别名
// ✅ 正确:使用路径别名
import { foo } from '@/utils/foo';
import type { Bar } from '#/types';
// ❌ 错误:不要使用旧版别名
import { foo } from '/@/utils/foo';导入顺序
// 1. Vue 相关
import { ref, computed } from 'vue';
// 2. 第三方库
import { message } from 'ant-design-vue';
// 3. 内部模块(使用别名)
import { useUserStore } from '@/store/modules/user';
import type { UserInfo } from '#/types';
// 4. 相对路径导入
import './styles.css';样式规范
使用 UnoCSS
<template>
<!-- ✅ 推荐:使用 UnoCSS 原子类 -->
<div class="flex items-center gap-4 p-4">
<span class="text-lg font-bold">Title</span>
</div>
</template>避免内联样式
<template>
<!-- ❌ 避免:内联样式 -->
<div style="color: red; margin: 10px;">
<!-- ✅ 推荐:使用 class -->
<div class="text-red-500 m-2.5">
</template>API 调用规范
使用 defHttp
import { defHttp } from '@/utils/http/axios';
// ✅ 推荐:使用封装的 defHttp
const result = await defHttp.get<UserInfo>({
url: Api.GET_USER_INFO,
params: { id },
});
// ❌ 避免:直接使用 axios
import axios from 'axios';
const result = await axios.get('/api/user');状态管理规范
使用 Pinia
// ✅ 推荐:使用 Pinia store
import { useUserStore } from '@/store/modules/user';
const userStore = useUserStore();
const { userInfo } = storeToRefs(userStore);
// ✅ 推荐:解构时保持响应性
userStore.updateUserInfo(data);项目结构
src/
├── api/ # API 接口
├── assets/ # 静态资源
├── components/ # 全局组件
├── design/ # 样式相关
├── hooks/ # 组合式函数
├── layouts/ # 布局组件
├── locales/ # 国际化
├── logics/ # 业务逻辑
├── router/ # 路由配置
├── settings/ # 项目配置
├── store/ # 状态管理
├── types/ # 类型定义
├── utils/ # 工具函数
└── views/ # 页面视图相关文档
- 03. 前端配置说明 - 环境变量与配置
- 12. 项目构建 - 构建与部署
- 11. 依赖说明 - 依赖包说明
页面信息›
已优化是