pnpm
/ yarn
/ npm
,但应锁定锁文件版本,避免跨环境依赖变更问题。src/
├── assets/ # 静态资源(图片、字体等)
├── components/ # 全局/通用组件
├── views/ # 页面组件
├── layout/ # 布局组件
├── router/ # 路由配置
├── store/ # 状态管理(视项目使用)
├── api/ # API 封装模块
├── types/ # 全局类型定义
├── utils/ # 通用工具方法
├── directives/ # 自定义指令(可选)
├── hooks/ # 封装的逻辑函数(composables)
|—— plugins/ # Vue插件注入管理
└── App.vue
说明:
views/模块
,下设子页面与本地组件。components/
,保持复用性。<script setup>
语法;<script setup lang="ts">
...
</script>
<template>
<div>
...
</div>
</template>
<style scoped>
...
</style>
类型 | 命名示例 | 说明 |
---|---|---|
组件名 | UserCard |
大驼峰,尽量名词化 |
变量名 | userList |
小驼峰 |
方法名 | fetchUserList |
动宾结构,小驼峰 |
文件名 | user-card.vue |
推荐使用中划线风格( kebab-case ) |
scoped
样式隔离组件作用域;具体样式方案视项目选择:
UnoCss | Tailwind CSS(如需快速构建响应式界面) |
建议在团队层面保持统一风格:
.btn {
@apply text-white bg-primary px-4 py-2 rounded;
}
el-form
配合 YUP 等库进行扩展校验。推荐 ESLint 规则:
eslint:recommended
@typescript-eslint/recommended
plugin:vue/vue3-recommended
.eslintrc.js
和 .prettierrc
文件统一格式;推荐封装统一请求工具:
// api/http.ts
const service = axios.create({
baseURL: import.meta.env.VITE_API_BASE,
timeout: 10000,
});
// 添加请求/响应拦截器
service.interceptors.response.use(
res => res.data,
err => {
ElMessage.error(err.message);
return Promise.reject(err);
}
);
export default service;
types/
中;defineProps<T>()
、withDefaults()
等方式增强类型安全性。hooks/useXXX.ts
或 utils/xxx.ts
;// bad
const user_name = ''
// good
const userName = ''
// bad
const userName = ref()
// good
const userName = ref('')
// good
const userName = ref<string>()
// bad
const handleCheckboxChange = (item, index) => {
for (let i = 0; i < checked.value.length; i++) {
if (i !== index) {
checked.value[i] = false;
}
}
productPackage.value = item;
};
// good
const handleCheckboxChange = (item, index) => {
checked.value = checked.value.map((_, i) => i === index);
productPackage.value = item;
};
// bad
// 统计输入的字数
const handleInput = () => {};
// good
/** 统计输入的字数 */
const handleInput = () => {};
// bad
const getProductPackage = () => {
return getProductPackageList({ productId: id }).then((res) => {
productPackageList.value = res;
});
};
// good
const getProductPackage = async () => {
productPackageList.value = await getProductPackageList({ productId: id })
};
// bad
<td v-if="item.useCount !== null"></td>
<td v-else>不限查询次数</td>
// good
<td></td>
// good
<td></td>
// bad
const user = ref({
name: 'xxx',
age: 16
})
// good
const user = reactive({
name: 'xxx',
age: 16
})
// bad
const createAppDialogVisiable = ref(false);
const appDeatailDialogVisiable = ref(false);
const dialogVisiable = ref(false);
const resertDialogVisible = ref(false);
// good
const dialogVisible = reactive({
createApp: false,
appDeatail: false,
dialog: false,
resert: false,
})
// bad
{
path: '/dashboard/product/manage',
name: 'ProductOrder',
component: () => import('@/views/dashboard/product/index.vue'),
},
// good
{
path: '/dashboard/product/manage',
name: 'ProductManage',
component: () => import('@/views/dashboard/product/manage/index.vue'),
},
// good
{
path: '/dashboard/product/manage/index',
name: 'ProductManage',
component: () => import('@/views/dashboard/product/manage/index.vue'),
},
使用props接收路由参数
// bad 无法知道 params 里包含的参数
const route = useRoute();
const id = route.params.id;
// good 明确当前页面参数依赖
const props = defineProps<{
id: string
}>()
TODO 代码中尚未完成的部分或者开发者计划在未来添加或修改的功能。
NOTE 用于提供额外的信息或解释某些决策的原因。
HACK 标记那些可能不是最优解,但为了特定目的暂时采用的解决方案。这种标记通常用来指出代码中可能存在的临时性或非标准做法。
XXX 通常表示不确定或有问题的地方,可能需要进一步检查或修改。
INFO 类似于”NOTE”,用于提供信息性的备注。
WARNING 指出潜在的问题或需要特别注意的地方
REVIEW 表示该段代码需要经过复审,尤其是在代码合并之前。
OPTIMIZE 标记那些可以进行优化的部分。
❌ 错误的注释标记:
// TODO 这里需要优化一下
✅ 正确的注释标记:
/**
* TODO: 这里需要优化一下,当数据量大时可能会有性能问题
*/
any
,除非有必要;