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 ❌
// 通过id获取字段列表
function getFieldListById() {}
Good ✅
class FieldItem {}
const FieldItem = {
id: null,
name: null
}
Bad ❌
class fieldItem {}
const Fielditem = {}
Good ✅
const styleConfig = {}
function styleConfig () {}
Bad ❌
const StyleConfig = {}
function Styleconfig () {}
关于对应关系不确定的词,禁止使用缩写
Good ✅
const dataSource = {}
function mathJs () {}
const newValue = {}
Bad ❌
const ds = {}
function mathJavaScript () {}
const newVal = {}
Good ✅
const BAR_CHART_LENGTH = 70;
const BAR_CHART_TYPE_CHART = "bar";
const BAR_CHART_TYPE_CHART = symbol("bar");
Bad ❌
const barChartLength = 70;
const Bar_chart_CHART = "bar";
const BAR_chart_type_Symbol = symbol("bar");
Good ✅
const options = {
field: {
id: "",
name: ""
}
}
Bad ❌
const options = {
field: {
fieldId: "",
fieldName: ""
}
}
Good ✅
props: {
status: {
type: String,
required: true
}
}
Bad ❌
props: ['status']
组件中使用到的 hook 函数统一放到同级目录下 hook.js
文件中
- src
- components
- Table
- hooks.js // Table 组件使用的所有 hook 函数放到这里
- index.vue
- hooks // 有可能全局使用的 hook 函数放到这里
- views
Good ✅
const useTask = () => {
const $hasJob = ref(false);
return { $hasJob }
}
Bad ❌
const getTask = () => {
const hasJob = ref(false);
return { hasJob }
}
为了提醒使用变量中的 value
属性获取元素,请使用 ref
包装的响应式变量使用 $
开头.
Good ✅
const useTask = () => {
const $hasJob = ref(false);
return { $hasJob }
}
Bad ❌
const useTask = () => {
const hasJob = ref(false);
return { hasJob }
}
由于在组件中使用 ref
变量不需要使用 value
,所以在 setup
方法返回到外部时需要去掉 $
.
Good ✅
export default {
setup() {
const { $hasJob } = useTask();
return {
hasJob: $hasJob
}
}
}
Bad ❌
export default {
setup() {
const { $hasJob } = useTask();
return {
$hasJob
}
}
}
如需要,需使用 mapActions / mapGetters 显示声明
Good ✅
import { mapGetters } from "vuex"
export default {
computed: mapGetters("xList")
}
bad ❌
{
methods: {
computeXList() {
return this.$store.xList;
}
}
}
Good ✅
{
methods: {
mapActions([ "queryFields" ]),
init() {
this.queryFields()
}
}
}
Bad ❌
this.$store.dispatch("queryFields", window.innerHeight);
- 不可以直接commit 调用 mutation
Good ✅
{
methods: {
mapActions([ "queryFields" ]),
init() {
this.queryFields()
}
}
}
Bad ❌
this.$store.commit("queryFields", window.innerHeight);
or
{
methods: {
mapMutations([ "queryFields" ]),
init() {
this.queryFields()
}
}
}
Mutation 需在 Action 中 commit,页面只能 dispatch action
Good ✅
{
getters: {
currentId(state) {
return state.id
}
}
}
Bad ❌
{
getters: {
currentId(state) {
state.type = state.list[state.id]
return state.id
}
}
}