vue.js

关注公众号 jb51net

关闭
首页 > 网络编程 > JavaScript > javascript类库 > vue.js > Vue项目命名规范指南

Vue项目命名规范指南分享

作者:SanMu三木

本文提供Vue3+Pinia+VueRouter项目命名规范,涵盖组件、路由、状态等命名风格,推荐使用ESLint和Prettier工具,统一风格提升协作效率

Vue项目命名规范指南(适用于 Vue 3 + Pinia + Vue Router)

目的:统一命名风格,提升可读性、可维护性和团队协作效率。

一、通用原则

类型命名风格示例
变量camelCaseuserName, isLoading
常量UPPER_SNAKE_CASEMAX_RETRY_COUNT, DEFAULT_TIMEOUT
函数/方法camelCasefetchData, validateForm
类 / 组件 / 枚举 / 路由 namePascalCaseUserProfile, ErrorBoundary
自定义指令(Vue)kebab-casev-focus, v-tooltip
URL 路径kebab-case/user-profile, /settings/account

二、组件相关命名规范

1. 组件文件名

components/
  UserProfile.vue        ✅ 推荐
  userProfile.vue        ❌ 不推荐(容易与变量混淆)
  user-profile.vue       ✅ 在模板中使用时推荐

2. 组件注册与使用

import UserProfile from './components/UserProfile.vue'
<template>
  <user-profile :user="user" />
</template>

3. Props 命名(组件间传参)

props: {
  userName: String,
  isActive: Boolean,
  userId: Number
}
<template>
  <UserCard :user-name="name" :is-active="true" />
</template>

三、Vue Router 相关命名规范

1. 路由路径(URL)

{
  path: '/user-profile',
  name: 'UserProfile',
  component: () => import('../views/UserProfile.vue')
}

2. 路由名称(name)

name: 'UserProfile'

3. 动态参数命名

path: '/user/:user_id' // URL 中使用
$route.params.userId    // JS 中自动转为 camelCase

四、Pinia 状态管理命名规范

1. Store 文件名

stores/
  userStore.js
  cartStore.js

2. State 属性命名

state: () => ({
  user: null,
  isLoading: false,
  activeTab: 'dashboard'
})

3. Actions 命名

actions: {
  async fetchUser(userId) {},
  updateUser(userData) {}
}

4. Getters 命名

getters: {
  isLoggedIn: (state) => !!state.user,
  fullName: (state) => `${state.firstName} ${state.lastName}`
}

五、变量与函数命名规范

1. 变量命名

let currentUser = null;
let loadingCount = 0;

2. 函数命名

function validateForm() {}
function submitData(data) {}

3. 布尔类型变量命名

let isActive = false;
let hasPermission = true;

六、常量命名规范

const MAX_RETRY_COUNT = 3;
const USER_ROLES = ['admin', 'editor', 'viewer'];

七、事件命名规范(Vue 组件通信)

this.$emit('update:loading', false);
<template>
  <ChildComponent @update:loading="handleLoadingChange" />
</template>

八、自定义指令命名规范

app.directive('focus', {
  mounted(el) {
    el.focus();
  }
});

使用方式:

<input v-focus />

九、样式类命名建议(可选)

.user-card {}
.user-card__header {}
.user-card--active {}

十、模块化命名建议

如果你的项目采用模块化结构(如多个 store、路由模块、功能模块),建议在命名中加入上下文前缀。

示例:

类型示例
StoreuserStore.js, productStore.js
路由模块userRoutes.js, orderRoutes.js
组件UserList.vue, UserDetail.vue

十一、附录:命名风格总结表

类型命名风格示例
组件名PascalCaseUserProfile.vue
模板标签kebab-case<user-profile />
PropscamelCase(JS) / kebab-case(模板)userName, user-name
路由 namePascalCase'UserProfile'
路由路径kebab-case/user-profile
动态路由参数kebab-case(URL) / camelCase(JS):user_id → params.userId
变量camelCaseuserName, isLoading
常量UPPER_SNAKE_CASEMAX_RETRY_COUNT
函数camelCasefetchData, submitForm
布尔变量is/has/should + 描述isActive, hasPermission
Pinia statecamelCaseuser, cartItems
Pinia actions动词 + 名词fetchUser, addToCart
Pinia getters名词/形容词isLoggedIn, totalPrice
自定义指令kebab-casev-focus

十二、工具建议(可选)

总结

一个良好的命名规范能显著提升代码质量与团队协作效率。建议将此文档纳入项目初期的技术规范文档,并结合 ESLint/Prettier 工具进行自动化检查。

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。

您可能感兴趣的文章:
阅读全文