Vue項目命名規(guī)范指南分享
Vue項目命名規(guī)范指南(適用于 Vue 3 + Pinia + Vue Router)
目的:統(tǒng)一命名風(fēng)格,提升可讀性、可維護性和團隊協(xié)作效率。
一、通用原則
| 類型 | 命名風(fēng)格 | 示例 |
|---|---|---|
| 變量 | camelCase | userName, isLoading |
| 常量 | UPPER_SNAKE_CASE | MAX_RETRY_COUNT, DEFAULT_TIMEOUT |
| 函數(shù)/方法 | camelCase | fetchData, validateForm |
| 類 / 組件 / 枚舉 / 路由 name | PascalCase | UserProfile, ErrorBoundary |
| 自定義指令(Vue) | kebab-case | v-focus, v-tooltip |
| URL 路徑 | kebab-case | /user-profile, /settings/account |
二、組件相關(guān)命名規(guī)范
1. 組件文件名
- 使用 PascalCase 或 KebabCase。
- 推薦使用 PascalCase,便于在 JS 中直接引用。
components/ UserProfile.vue ? 推薦 userProfile.vue ? 不推薦(容易與變量混淆) user-profile.vue ? 在模板中使用時推薦
2. 組件注冊與使用
- 注冊時使用 PascalCase:
import UserProfile from './components/UserProfile.vue'
- 模板中使用 kebab-case:
<template> <user-profile :user="user" /> </template>
3. Props 命名(組件間傳參)
- 使用 camelCase(JS 中),模板中用 kebab-case。
- 避免縮寫(除非是通用縮寫如
id,url)。
props: {
userName: String,
isActive: Boolean,
userId: Number
}
<template> <UserCard :user-name="name" :is-active="true" /> </template>
三、Vue Router 相關(guān)命名規(guī)范
1. 路由路徑(URL)
- 使用 kebab-case,清晰表達資源結(jié)構(gòu)。
{
path: '/user-profile',
name: 'UserProfile',
component: () => import('../views/UserProfile.vue')
}
2. 路由名稱(name)
- 使用 PascalCase,與組件名保持一致。
name: 'UserProfile'
3. 動態(tài)參數(shù)命名
- 參數(shù)名使用 kebab-case(URL),JS 中訪問使用 camelCase。
path: '/user/:user_id' // URL 中使用 $route.params.userId // JS 中自動轉(zhuǎn)為 camelCase
四、Pinia 狀態(tài)管理命名規(guī)范
1. Store 文件名
- 使用小駝峰命名,以
.store.js或.store.ts結(jié)尾。
stores/ userStore.js cartStore.js
2. State 屬性命名
- 使用 camelCase,語義明確。
state: () => ({
user: null,
isLoading: false,
activeTab: 'dashboard'
})
3. Actions 命名
- 動詞開頭 + 名詞,表示動作。
actions: {
async fetchUser(userId) {},
updateUser(userData) {}
}
4. Getters 命名
- 名詞或形容詞為主,表示派生狀態(tài)。
getters: {
isLoggedIn: (state) => !!state.user,
fullName: (state) => `${state.firstName} ${state.lastName}`
}
五、變量與函數(shù)命名規(guī)范
1. 變量命名
- 使用 camelCase。
- 避免模糊命名(如
data,info),應(yīng)具體說明用途。
let currentUser = null; let loadingCount = 0;
2. 函數(shù)命名
- 動詞開頭,描述行為。
function validateForm() {}
function submitData(data) {}
3. 布爾類型變量命名
- 以
is,has,should開頭。
let isActive = false; let hasPermission = true;
六、常量命名規(guī)范
- 使用全大寫 + 下劃線分隔。
- 通常用于配置項、枚舉、固定值等。
const MAX_RETRY_COUNT = 3; const USER_ROLES = ['admin', 'editor', 'viewer'];
七、事件命名規(guī)范(Vue 組件通信)
- 使用 kebab-case,在子組件中
$emit時使用:
this.$emit('update:loading', false);
- 在父組件中監(jiān)聽:
<template> <ChildComponent @update:loading="handleLoadingChange" /> </template>
八、自定義指令命名規(guī)范
- 使用 kebab-case,符合 HTML 規(guī)范。
app.directive('focus', {
mounted(el) {
el.focus();
}
});
使用方式:
<input v-focus />
九、樣式類命名建議(可選)
- 推薦使用 BEM 風(fēng)格或 CSS-in-JS 工具。
- 如果使用 SCSS/CSS,建議命名清晰:
.user-card {}
.user-card__header {}
.user-card--active {}
十、模塊化命名建議
如果你的項目采用模塊化結(jié)構(gòu)(如多個 store、路由模塊、功能模塊),建議在命名中加入上下文前綴。
示例:
| 類型 | 示例 |
|---|---|
| Store | userStore.js, productStore.js |
| 路由模塊 | userRoutes.js, orderRoutes.js |
| 組件 | UserList.vue, UserDetail.vue |
十一、附錄:命名風(fēng)格總結(jié)表
| 類型 | 命名風(fēng)格 | 示例 |
|---|---|---|
| 組件名 | PascalCase | UserProfile.vue |
| 模板標簽 | kebab-case | <user-profile /> |
| Props | camelCase(JS) / kebab-case(模板) | userName, user-name |
| 路由 name | PascalCase | 'UserProfile' |
| 路由路徑 | kebab-case | /user-profile |
| 動態(tài)路由參數(shù) | kebab-case(URL) / camelCase(JS) | :user_id → params.userId |
| 變量 | camelCase | userName, isLoading |
| 常量 | UPPER_SNAKE_CASE | MAX_RETRY_COUNT |
| 函數(shù) | camelCase | fetchData, submitForm |
| 布爾變量 | is/has/should + 描述 | isActive, hasPermission |
| Pinia state | camelCase | user, cartItems |
| Pinia actions | 動詞 + 名詞 | fetchUser, addToCart |
| Pinia getters | 名詞/形容詞 | isLoggedIn, totalPrice |
| 自定義指令 | kebab-case | v-focus |
十二、工具建議(可選)
- 使用 ESLint 和 Prettier 來自動格式化和校驗命名風(fēng)格。
- 使用 Vue Language Features (Volar) 提升 Vue 開發(fā)體驗。
- 使用 TypeScript 增強類型安全和命名一致性。
總結(jié)
一個良好的命名規(guī)范能顯著提升代碼質(zhì)量與團隊協(xié)作效率。建議將此文檔納入項目初期的技術(shù)規(guī)范文檔,并結(jié)合 ESLint/Prettier 工具進行自動化檢查。
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
17個vue常用的數(shù)組方法總結(jié)與實例演示
這篇文章主要介紹了vue中常用的數(shù)組方法,包括:VUE數(shù)組轉(zhuǎn)換字符串,VUE數(shù)組遍歷,VUE數(shù)組過濾,VUE數(shù)組查詢,VUE數(shù)組排序等功能,需要的朋友可以參考下2022-12-12
Vue3使用Proxy構(gòu)建高效響應(yīng)式數(shù)據(jù)的示例代碼
在 Vue 3 中,Proxy 主要用于 攔截對象的基本操作,包括 屬性讀?。╣et)、修改(set)、刪除(deleteProperty) 等,本文給大家介紹了Vue3使用Proxy構(gòu)建高效響應(yīng)式數(shù)據(jù)的操作教程,需要的朋友可以參考下2025-03-03

