Vue(五)Vue規范


代碼規范很重要

1.組件名應該始終是多個單詞的,根組件 App 除外。

2.組件的 data 必須是一個函數。

// In a .vue file
export default {
  data () {
    return {
      foo: 'bar'
    }
  }
}

3.Prop 定義應該盡量詳細。【包含了類型、校驗】

// 更好的做法!
props: {
  status: {
    type: String,
    required: true,
    validator: function (value) {
      return [
        'syncing',
        'synced',
        'version-conflict',
        'error'
      ].indexOf(value) !== -1
    }
  }
}

類型有:

單向數據流:所有的 prop 都使得其父子 prop 之間形成了一個單向下行綁定:父級 prop 的更新會向下流動到子組件中,但是反過來則不行。

4.總是用 key 配合 v-for

<ul>
  <li
    v-for="todo in todos"
    :key="todo.id"
  >
    {{ todo.text }}
  </li>
</ul>

5.永遠不要把 v-if 和 v-for 同時用在同一個元素上。

6.對於應用來說,頂級 App 組件和布局組件中的樣式可以是全局的,但是其它所有組件都應該是有作用域的。

7.在插件、混入等擴展中始終為自定義的私有屬性使用 $_ 前綴。並附帶一個命名空間以回避和其它作者的沖突 (比如 $_yourPluginName_)。

var myGreatMixin = {
  // ...
  methods: {
    $_myGreatMixin_update: function () {
      // ...
    }
  }
}

 

8.只要有能夠拼接文件的構建系統,就把每個組件單獨分成文件

// 反例
Vue.component('TodoList', {
  // ...
})

Vue.component('TodoItem', {
  // ...
})

// 好例子
components/
|- TodoList.js
|- TodoItem.js
components/
|- TodoList.vue
|- TodoItem.vue

 

9.單文件組件的文件名應該要么始終是單詞大寫開頭 (PascalCase),要么始終是橫線連接 (kebab-case)。

// 反例
components/
|- mycomponent.vue
components/
|- myComponent.vue

// 好例子
components/
|- MyComponent.vue
components/
|- my-component.vue

 

10.應用特定樣式和約定的基礎組件 (也就是展示類的、無邏輯的或無狀態的組件) 應該全部以一個特定的前綴開頭,比如 Base、App 或 V。

// 反例
components/
|- MyButton.vue
|- VueTable.vue
|- Icon.vue
// 好例子
components/
|- BaseButton.vue
|- BaseTable.vue
|- BaseIcon.vue
components/
|- AppButton.vue
|- AppTable.vue
|- AppIcon.vue
components/
|- VButton.vue
|- VTable.vue
|- VIcon.vue

 

11.只應該擁有單個活躍實例的組件應該以 The 前綴命名,以示其唯一性。

// 反例
components/
|- Heading.vue
|- MySidebar.vue

// 好例子
components/
|- TheHeading.vue
|- TheSidebar.vue

12.和父組件緊密耦合的子組件應該以父組件名作為前綴命名。

13.組件名應該以高級別的 (通常是一般化描述的) 單詞開頭,以描述性的修飾詞結尾

14.在單文件組件、字符串模板和 JSX 中沒有內容的組件應該是自閉合的——在 DOM 模板里永遠不要這樣做。

反例
<!-- 在單文件組件、字符串模板和 JSX 中 -->
<MyComponent></MyComponent>
<!-- 在 DOM 模板中 -->
<my-component/>
好例子
<!-- 在單文件組件、字符串模板和 JSX 中 -->
<MyComponent/>
<!-- 在 DOM 模板中 -->
<my-component></my-component>

15.對於絕大多數項目來說,在單文件組件和字符串模板中組件名應該總是 PascalCase 的——但是在 DOM 模板中總是 kebab-case 的。

反例
<!-- 在單文件組件和字符串模板中 -->
<mycomponent/>
<!-- 在單文件組件和字符串模板中 -->
<myComponent/>
<!-- 在 DOM 模板中 -->
<MyComponent></MyComponent>
好例子
<!-- 在單文件組件和字符串模板中 -->
<MyComponent/>
<!-- 在 DOM 模板中 -->
<my-component></my-component>
或者
<!-- 在所有地方 -->
<my-component></my-component>

16.盡管在較為簡單的應用中只使用 Vue.component 進行全局組件注冊時,可以使用 kebab-case 字符串。但是JS/JSX 中的組件名應該始終是 PascalCase 的

// 反例
Vue.component('myComponent', {
  // ...
})
import myComponent from './MyComponent.vue'
export default {
  name: 'myComponent',
  // ...
}
export default {
  name: 'my-component',
  // ...
}
// 好例子
Vue.component('MyComponent', {
  // ...
})
Vue.component('my-component', {
  // ...
})
import MyComponent from './MyComponent.vue'
export default {
  name: 'MyComponent',
  // ...
}

 

17.組件名應該傾向於完整單詞而不是縮寫。

18.在聲明 prop 的時候,其命名應該始終使用 camelCase,而在模板和 JSX 中應該始終使用 kebab-case

// 反例
props: {
  'greeting-text': String
}
<WelcomeMessage greetingText="hi"/>
// 好例子
props: {
  greetingText: String
}
<WelcomeMessage greeting-text="hi"/>

 

19.多個屬性的元素應該分多行撰寫,每個屬性一行。

<!--反例-->
<img src="https://vuejs.org/images/logo.png" alt="Vue Logo">
<MyComponent foo="a" bar="b" baz="c"/>
<!--好例子-->
<img
  src="https://vuejs.org/images/logo.png"
  alt="Vue Logo"
>
<MyComponent
  foo="a"
  bar="b"
  baz="c"
/>

20.組件模板應該只包含簡單的表達式,復雜的表達式則應該重構為計算屬性(Computed)或方法(Methods)

// 反例
{{
  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(' ')
  }
}

21.應該把復雜計算屬性分割為盡可能多的更簡單的屬性。

// 反例
computed: {
  price: function () {
    var basePrice = this.manufactureCost / (1 - this.profitMargin)
    return (
      basePrice -
      basePrice * (this.discountPercent || 0)
    )
  }
}
// 好例子
computed: {
  basePrice: function () {
    return this.manufactureCost / (1 - this.profitMargin)
  },
  discount: function () {
    return this.basePrice * (this.discountPercent || 0)
  },
  finalPrice: function () {
    return this.basePrice - this.discount
  }
}

22.非空 HTML 屬性值應該始終帶引號 (單引號或雙引號,選你 JS 里不用的那個)。

<!--反例-->
<input type=text>
<AppSidebar :style={width:sidebarWidth+'px'}>
<!--好例子-->
<input type="text">
<AppSidebar :style="{ width: sidebarWidth + 'px' }">

23.指令縮寫 (用 : 表示 v-bind: 和用 @ 表示 v-on:) 應該要么都用要么都不用

24.單文件組件應該總是讓 <script>、<template> 和 <style> 標簽的順序保持一致。且 <style> 要放在最后,因為另外兩個標簽至少要有一個。

好例子
<!-- ComponentA.vue -->
<script>/* ... */</script>
<template>...</template>
<style>/* ... */</style>

<!-- ComponentA.vue -->
<template>...</template>
<script>/* ... */</script>
<style>/* ... */</style>

25.如果一組 v-if + v-else 的元素類型相同,最好使用 key (比如兩個 <div> 元素)。或者讓他們兩個元素類型不同。

<!--反例-->
<div v-if="error">
  錯誤:{{ error }}
</div>
<div v-else>
  {{ results }}
</div>
<!--好例子-->
<div
  v-if="error"
  key="search-status"
>
  錯誤:{{ error }}
</div>
<div
  v-else
  key="search-results"
>
  {{ results }}
</div>

<p v-if="error">
  錯誤:{{ error }}
</p>
<div v-else>
  {{ results }}
</div>

 

26.在 scoped 樣式中,類選擇器比元素選擇器更好,因為大量使用元素選擇器是很慢的。

27.應該優先通過 Vuex 管理全局狀態,而不是通過 this.$root 或一個全局事件總線。

28.自定義事件:始終使用 kebab-case 的事件名

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM