Vue风格指南
文件命名
使用大驼峰
js
// good
EntityList.vue
entityList.vue
组件名为多个单词
组件名应该始终是多个单词的,根组件 App 以及 <transition>
、<component>
之类的 Vue 内置组件除外。
这样做可以避免跟现有的以及未来的 HTML 元素相冲突,因为所有的 HTML 元素名称都是单个单词的。
vue
// good
Vue.component('todo-item', {
// ...
})
export default {
name: 'TodoItem',
// ...
}
自闭合组件
在单文件组件中没有内容的组件应该是自闭合的
vue
<!-- bad -->
<u-input></u-input>
<!-- good -->
<u-input />
指令缩写
用:
表示 v-bind
: ,用@
表示v-on
vue
<!-- bad -->
<input v-bind:value="value" v-on:input="onInput">
<!-- good -->
<input :value="value" @input="onInput">
永远不要把 v-if
和 v-for
同时用在同一个元素上
vue
<ul v-if="shouldShowUsers">
<li
v-for="user in users"
:key="user.id"
>
{{ user.name }}
</li>
</ul>
避免使用元素选择器
尽量使用类选择器来设置样式
组件数据
组件的 data 必须是一个函数,并且建议在此不使用箭头函数
vue
// bad
export default {
data: () => ({
foo: 'bar'
})
}
// good
export default {
data () {
return {
foo: 'bar'
}
}
}
props定义
小驼峰命名。内容尽量详细,至少有默认值
vue
// bad
greeting-text: String
// good
greetingText: { type: String, default: ''}
组件属性顺序和分行规则
顺序原则:重要属性放前面
顺序依据:依次指令
->props属性
-> 事件
->dom属性(class有标记作用,除外)
分行规则:放在一行,重要内容较多时,可放置2~3行
vue
<!-- bad -->
<u-select
class="select"
size="s"
@select="searchEntity($event, row)"
@blur="searchEntity($event, row)"
v-model="row.variableId"
:list="variableList" />
<!-- good -->
<u-select v-model="row.variableId" :list="variableList" size="s"
@select="searchEntity($event, row)" @blur="searchEntity($event, row)" class="select" />
Vue API顺序
js
export default {
name: '',
/*1. Vue扩展 */
extends: '', // extends和mixins都扩展逻辑,需要重点放前面
mixins: [],
components: {},
/* 2. Vue数据 */
props: {},
model: { prop: '', event: '' }, // model 会使用到 props
data () {
return {}
},
computed: {},
watch:{}, // watch 监控的是 props 和 data,有必要时监控computed
/* 3. Vue资源 */
filters: {},
directives: {},
/* 4. Vue生命周期 */
created () {},
mounted () {},
destroy () {},
/* 5. Vue方法 */
methods: {}, // all the methods should be put here in the last
}
Vue组件顶级标签顺序
顺序保持一致,且标签之间留有空行。
vue
<template>
<div></div>
</template>
<script>
export default {}
</script>
<style>
.app {}
</style>
import引入顺序
原则:同等类型的放一起,优先放mixins和components等UI资源。
vue
// bad
import { getAllEntityList, getVariableGroup } from '@/server/api'
import { helpers } from 'vuelidate/lib/validators'
import { getRepeatLine } from '@/utils/common'
import { CloseModalMixin, InvalidCheckMixin } from '@/components/common/mixins'
import VSearchSelect from '@/components/variable/v-search-select'
import EModifyModal from '@/components/entity/e-modify-modal'
import { MODIFY_MODAL_TYPE } from '@/utils/config'
import { botIdLoc, custIdLoc } from '@/utils/locs'
// good
import { CloseModalMixin, InvalidCheckMixin } from '@/components/common/mixins'
import VSearchSelect from '@/components/variable/v-search-select'
import EModifyModal from '@/components/entity/e-modify-modal'
import { getAllEntityList, getVariableGroup } from '@/server/api'
import { helpers } from 'vuelidate/lib/validators'
import { MODIFY_MODAL_TYPE } from '@/utils/config'
import { getRepeatLine } from '@/utils/common'
import { botIdLoc, custIdLoc } from '@/utils/locs'
Vue 复杂data加注释/分组
data数据是连接View和Modal的基础,当ViewModal复杂时,建议进行注释并分组。另外,当data过于复杂时应考虑优化重构。
js
// bad
data() {
return {
isOpenModal: false,
list: [],
groupList: [],
searchParams: { groupId: '', searchParam: '', searchType: '' },
pageParam: { currentPage: 1, pageSize: 50 },
totalCount: 0,
groupId: '',
typeList: [],
defaultType: 'paramName'
}
}
// good
data() {
return {
variableList: [],
groupList: [],
typeList: [],
/*
* 查询参数
* 组与组之间通过空行区分
*/
searchParams: { groupId: '', searchParam: '', searchType: '', currentPage: 1, pageSize: 50 },
totalCount: 0,
defaultType: '',
isOpenModal: false
}
}