Vue 编码规范
基本约束
单个JS / Vue 文章代码不能超过 800行
800行禁止提交非必要 console.log
console.logESLint 规则必须通过才能提交
ESLint 规则必须通过才能提交变量 / 方法名有拼写错误需修改
能使用 Lodash 的方法不要自己写
Good ✅
const newValue = cloneDeep(oldValue)Bad ❌
const newValue = { ...oldValue }只添加必要注释
如无必要,不要加注释,如有必要,必须加注释。
Good ✅
// Check if an incoming prop key is a declared emit event listener.
// e.g. With `emits: { click: null }`, props named `onClick` and `onclick` are
// both considered matched listeners.
export function isEmitListener(comp: Component, key: string): boolean { ... }Bad ❌
命名规范
class / type 名需要大写开头
Good ✅
Bad ❌
方法 / 对象 要采用驼峰命名
Good ✅
Bad ❌
如对应关系未获得广泛认可,禁止使用缩写
关于对应关系不确定的词,禁止使用缩写
Good ✅
Bad ❌
数值 / 字符等基础数据类型常量,使用全大写
Good ✅
Bad ❌
注意对象层级结构,不要重复描述
Good ✅
Bad ❌
Vue
Prop
非必填需提供默认值
定义应该尽量详细
Good ✅
Bad ❌
Composition API
目录结构
组件中使用到的 hook 函数统一放到同级目录下 hook.js 文件中
所有的 hook 函数必须使用 use 开头
use 开头Good ✅
Bad ❌
使用 CompositionAPI ref 开头的变量要使用 $ 开头
为了提醒使用变量中的 value 属性获取元素,请使用 ref 包装的响应式变量使用 $ 开头.
Good ✅
Bad ❌
ref 变量返回时需要去掉 $ 开头
由于在组件中使用 ref 变量不需要使用 value,所以在 setup 方法返回到外部时需要去掉 $.
Good ✅
Bad ❌
Vuex
禁止在页面中使用 $store API
如需要,需使用 mapActions / mapGetters 显示声明
读取 store 数据,需要通过 getter
getterGood ✅
bad ❌
分发Action 需要使用 mapActions
Good ✅
Bad ❌
不可以直接commit 调用 mutation
Good ✅
Bad ❌
or
页面中禁止直接调用 Mutation
Mutation 需在 Action 中 commit,页面只能 dispatch action
禁止在 Getters 中操作 Store 数据、dispatch & commit
Good ✅
Bad ❌
Last updated
Was this helpful?