vue风格指南:css
1. 组件名为多个单词 :
```
Vue.component('todo-item',{
//...
})html
export default{
name:"TodoItem"
//...
}
```vue
2. 组件的data必须是一个函数
除了new Vue外的任何地方,它的值必须是一个返回函数。当 data 的值是一个对象时,它会在这个组件的全部实例之间共享,这样致使各个组件data会互相影响编辑器
3. Prop定义 尽可能详细
至少指定类型。
```
// 这样作只有开发原型系统时能够接受
props: ['status'] //只在开发原型系统时能够接受ide
//好例子
props:{
Status:{
type:String,
required:true,
validator:function(value){
return [
'syncing',
'synced',
'version-conflict',
'error'
].indexOf(value) !==-1
}
}
}
```函数
4. 为v-for设置键值
```
<li v-for="todo in todos" :key="todo.id">{{todo.text}}</li>
```布局
5. 避免v-if和v-for用在一块儿ui
6. 为组件样式设置做用域
对于应用来讲,顶级 App 组件和布局组件中的样式能够是全局的,可是其它全部组件都应该是有做用域的。
设置做用域,也能够经过css modules this
对于组件库,咱们应该更倾向于选用基于 class 的策略而不是 scoped 特性。component
7. 私有属性名
使用模块做用域保持不容许外部访问的函数的私有性使用$_ 前缀。并附带一个命名空间以回避和其它做者的冲突 (好比 $_yourPluginName_)
```
var myGreatMixin = {
// ...
methods: {
$_myGreatMixin_update: function () {
// ...
}
}
}
export default myGreatMixin;
```
或者更好
```
var myGreatMixin = {
// ...
methods: {
publicMethod() {
// ...
myPrivateFunction()
}
}
}
function myPrivateFunction() {
// ...
}
export default myGreatMixin
```
8. 组件文件
把每一个组件单独分红文件
```很差的例子 写在一块儿了
Vue.component('TodoList', {
// ...
})
Vue.component('TodoItem', {
// ...
})
```
```好的例子
components/
|- TodoList.js
|- TodoItem.js
components/
|- TodoList.vue
|- TodoItem.vue
```
9. 单文件组件文件的大小写
单文件组件的文件名应该要么始终是单词大写开头 (PascalCase),要么始终是横线链接
```
components/
|- MyComponent.vue
components/
|- my-component.vue
```
10. 基础组件(公共组件)
应用特定样式和约定的基础组件 (也就是展现类的、无逻辑的或无状态的组件) 应该所有以一个特定的前缀开头,好比 Base、App 或 V。
```
components/
|- BaseButton.vue
|- BaseTable.vue
|- BaseIcon.vue
或
components/
|- AppButton.vue
|- AppTable.vue
|- AppIcon.vue
或
components/
|- VButton.vue
|- VTable.vue
|- VIcon.vue
```
11. 单例组件名
只应该拥有单个活跃实例的组件应该以 The 前缀命名,以示其惟一性。
不意味着组件只可用于一个单页面,而是每一个页面只使用一次。这些组件永远不接受任何 prop,由于它们是为你的应用定制的,而不是它们在你的应用中的上下文。若是你发现有必要添加 prop,那就代表这其实是一个可复用的组件
```
components/
|- TheHeading.vue
|- TheSidebar.vue
```
12. 紧密耦合的组件名
和父组件紧密耦合的子组件应该以父组件名做为前缀命名。
若是一个组件只在某个父组件的场景下有意义,这层关系应该体如今其名字上
``` 很差的示例
components/
|- TodoList.vue
|- TodoItem.vue
|- TodoButton.vue
```
``` 好的示例
components/
|- TodoList.vue
|- TodoListItem.vue
|- TodoListItemButton.vue
```
13. 组件名中的单词顺序
组件名应该以高级别的 (一般是通常化描述的) 单词开头,以描述性的修饰词结尾。
``` 很差的示例
components/
|- ClearSearchButton.vue
|- ExcludeFromSearchInput.vue
```
```好的示例
components/
|- SearchButtonClear.vue
|- SearchButtonRun.vue
```
14. JS/JSX 中的组件名大小写
JS/JSX 中的组件名应该始终是 PascalCase 的,
```好的示例
Vue.component('MyComponent', {
// ...
})
Vue.component('my-component', {
// ...
})
import MyComponent from './MyComponent.vue'
export default {
name: 'MyComponent',
// ...
}
```
15. 完整单词的组件名
组件名应该倾向于完整单词而不是缩写
编辑器中的自动补全已经让书写长命名的代价很是之低了,而其带来的明确性倒是很是宝贵的。不经常使用的缩写尤为应该避免。
```好的示例
components/
|- StudentDashboardSettings.vue
|- UserProfileOptions.vue
```
16. Prop 名大小写
在 JavaScript 中更天然的是 camelCase。而在 HTML 中则是 kebab-case。
```好的示例
props: {
greetingText: String
}
<WelcomeMessage greeting-text="hi"/>
```
```很差的示例
props: {
'greeting-text': String
}
<WelcomeMessage greetingText="hi"/>
```
17. 多个特性的元素
多个特性的元素应该分多行撰写,每一个特性一行。在 JavaScript 中,用多行分隔对象的多个属性是很常见的最佳实践,由于这样更易读
```好的示例
<MyComponent
foo="a"
bar="b"
baz="c"
/>
```
18. 模板中简单的表达式
组件模板应该只包含简单的表达式,复杂的表达式则应该重构为计算属性或方法
```很差的示例
{{
fullName.split(' ').map(function (word) {
return word[0].toUpperCase() + word.slice(1)
}).join(' ')
}}
```
```好的示例
<!-- 在模板中 -->
{{ normalizedFullName }}
// 复杂表达式已经移入一个计算属性
computed: {
normalizedFullName: function () {
return this.fullName.split(' ').map(function (word) {
return word[0].toUpperCase() + word.slice(1)
}).join(' ')
}
}
```
19. 简单的计算属性
应该把复杂计算属性分割为尽量多的更简单的属性。
```好的示例
computed: {
basePrice: function () {
return this.manufactureCost / (1 - this.profitMargin)
},
discount: function () {
return this.basePrice * (this.discountPercent || 0)
},
finalPrice: function () {
return this.basePrice - this.discount
}
}
```
20. 带引号的特性值
非空 HTML 特性值应该始终带引号
```很差的示例
<input type=text>
<AppSidebar :style={width:sidebarWidth+'px'}>
```
```好的示例
<input type="text">
<AppSidebar :style="{ width: sidebarWidth + 'px' }">
```
21. 组件/实例的选项的顺序
组件/实例的选项应该有统一的顺序
a. el
b. name , parent
c. functional (更改组件的类型)
d. delimiters, comments (模板修改器)
e. components , directives,filters
f. extends, mixins
g. model, props/propsData
h. data, computed
i. watch, 生命周期 beforeCreate,created, beforeMounted, mounted, beforeUpdate,updated,activated, deactivated , beforeDestroy, destroyed
j. methods(非响应式的属性)不依赖响应系统的实例属性
k. 渲染template/render, renderError
22. 元素特性的顺序
a. is
b. v-for
c. v-if/v-show/v-cloak
d. v-pre/v-once
e. id
f. ref/key
g. v-model
h. 其余特性(全部普通的绑定或未绑定的特性)
i. v-on
j. v-html/v-text
23. 若是一组 v-if + v-else 的元素类型相同,最好使用 key
```好的示例
<div
v-if="error"
key="search-status"
>
错误:{{ error }}
</div>
<div
v-else
key="search-results"
>
{{ results }}
</div>
```
24. scoped 中的元素选择器
在 scoped 样式中,类选择器比元素选择器更好,由于大量使用元素选择器是很慢的。
```很差的示例
<template>
<button>X</button>
</template>
<style scoped>
button {
background-color: red;
}
</style>
```
```好的示例
<template>
<button class="btn btn-close">X</button>
</template>
<style scoped>
.btn-close {
background-color: red;
}
</style>
```
25. 隐性的父子组件通讯
优先使用prop和事件进行父子组件通讯
26. 非Flux的全局状态管理 应该优先经过 Vuex 管理全局状态,而不是经过 this.$root 或一个全局事件总线