Vue(五)Vue规范
代碼規(guī)范很重要
1.組件名應(yīng)該始終是多個單詞的,根組件 App 除外。
2.組件的 data 必須是一個函數(shù)。
// In a .vue file export default {data () {return {foo: 'bar'}} }3.Prop 定義應(yīng)該盡量詳細。【包含了類型、校驗】
// 更好的做法! props: {status: {type: String,required: true,validator: function (value) {return ['syncing','synced','version-conflict','error'].indexOf(value) !== -1}} }類型有:
單向數(shù)據(jù)流:所有的 prop 都使得其父子 prop 之間形成了一個單向下行綁定:父級 prop 的更新會向下流動到子組件中,但是反過來則不行。
4.總是用 key 配合 v-for
<ul><liv-for="todo in todos":key="todo.id">{{ todo.text }}</li> </ul>5.永遠不要把 v-if 和 v-for 同時用在同一個元素上。
6.對于應(yīng)用來說,頂級 App 組件和布局組件中的樣式可以是全局的,但是其它所有組件都應(yīng)該是有作用域的。
7.在插件、混入等擴展中始終為自定義的私有屬性使用 $_ 前綴。并附帶一個命名空間以回避和其它作者的沖突 (比如 $_yourPluginName_)。
var myGreatMixin = {// ... methods: {$_myGreatMixin_update: function () {// ... }} }?
8.只要有能夠拼接文件的構(gòu)建系統(tǒng),就把每個組件單獨分成文件。
// 反例 Vue.component('TodoList', {// ... })Vue.component('TodoItem', {// ... })// 好例子 components/ |- TodoList.js |- TodoItem.js components/ |- TodoList.vue |- TodoItem.vue?
9.單文件組件的文件名應(yīng)該要么始終是單詞大寫開頭 (PascalCase),要么始終是橫線連接 (kebab-case)。
// 反例 components/ |- mycomponent.vue components/ |- myComponent.vue// 好例子 components/ |- MyComponent.vue components/ |- my-component.vue?
10.應(yīng)用特定樣式和約定的基礎(chǔ)組件 (也就是展示類的、無邏輯的或無狀態(tài)的組件) 應(yīng)該全部以一個特定的前綴開頭,比如 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.只應(yīng)該擁有單個活躍實例的組件應(yīng)該以 The 前綴命名,以示其唯一性。
// 反例 components/ |- Heading.vue |- MySidebar.vue// 好例子 components/ |- TheHeading.vue |- TheSidebar.vue12.和父組件緊密耦合的子組件應(yīng)該以父組件名作為前綴命名。
13.組件名應(yīng)該以高級別的 (通常是一般化描述的) 單詞開頭,以描述性的修飾詞結(jié)尾
14.在單文件組件、字符串模板和 JSX 中沒有內(nèi)容的組件應(yīng)該是自閉合的——但在 DOM 模板里永遠不要這樣做。
反例 <!-- 在單文件組件、字符串模板和 JSX 中 --> <MyComponent></MyComponent> <!-- 在 DOM 模板中 --> <my-component/> 好例子 <!-- 在單文件組件、字符串模板和 JSX 中 --> <MyComponent/> <!-- 在 DOM 模板中 --> <my-component></my-component>15.對于絕大多數(shù)項目來說,在單文件組件和字符串模板中組件名應(yīng)該總是 PascalCase 的——但是在 DOM 模板中總是 kebab-case 的。
反例 <!-- 在單文件組件和字符串模板中 --> <mycomponent/> <!-- 在單文件組件和字符串模板中 --> <myComponent/> <!-- 在 DOM 模板中 --> <MyComponent></MyComponent> 好例子 <!-- 在單文件組件和字符串模板中 --> <MyComponent/> <!-- 在 DOM 模板中 --> <my-component></my-component> 或者 <!-- 在所有地方 --> <my-component></my-component>16.盡管在較為簡單的應(yīng)用中只使用 Vue.component 進行全局組件注冊時,可以使用 kebab-case 字符串。但是JS/JSX 中的組件名應(yīng)該始終是 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.組件名應(yīng)該傾向于完整單詞而不是縮寫。
18.在聲明 prop 的時候,其命名應(yīng)該始終使用 camelCase,而在模板和 JSX 中應(yīng)該始終使用 kebab-case。
// 反例 props: {'greeting-text': String } <WelcomeMessage greetingText="hi"/> // 好例子 props: {greetingText: String } <WelcomeMessage greeting-text="hi"/>?
19.多個屬性的元素應(yīng)該分多行撰寫,每個屬性一行。
<!--反例--> <img src="https://vuejs.org/images/logo.png" alt="Vue Logo"> <MyComponent foo="a" bar="b" baz="c"/> <!--好例子--> <imgsrc="https://vuejs.org/images/logo.png"alt="Vue Logo" > <MyComponentfoo="a"bar="b"baz="c" />20.組件模板應(yīng)該只包含簡單的表達式,復(fù)雜的表達式則應(yīng)該重構(gòu)為計算屬性(Computed)或方法(Methods)。
// 反例 {{fullName.split(' ').map(function (word) {return word[0].toUpperCase() + word.slice(1)}).join(' ') }} // 好例子 <!-- 在模板中 --> {{ normalizedFullName }} // 復(fù)雜表達式已經(jīng)移入一個計算屬性 computed: {normalizedFullName: function () {return this.fullName.split(' ').map(function (word) {return word[0].toUpperCase() + word.slice(1)}).join(' ')} }21.應(yīng)該把復(fù)雜計算屬性分割為盡可能多的更簡單的屬性。
// 反例 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 屬性值應(yīng)該始終帶引號 (單引號或雙引號,選你 JS 里不用的那個)。
<!--反例--> <input type=text> <AppSidebar :style={width:sidebarWidth+'px'}> <!--好例子--> <input type="text"> <AppSidebar :style="{ width: sidebarWidth + 'px' }">23.指令縮寫 (用 : 表示 v-bind: 和用 @ 表示 v-on:) 應(yīng)該要么都用要么都不用。
24.單文件組件應(yīng)該總是讓 <script>、<template> 和 <style> 標(biāo)簽的順序保持一致。且 <style> 要放在最后,因為另外兩個標(biāo)簽至少要有一個。
好例子 <!-- 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> <!--好例子--> <divv-if="error"key="search-status" >錯誤:{{ error }} </div> <divv-elsekey="search-results" >{{ results }} </div><p v-if="error">錯誤:{{ error }} </p> <div v-else>{{ results }} </div>?
26.在 scoped 樣式中,類選擇器比元素選擇器更好,因為大量使用元素選擇器是很慢的。
27.應(yīng)該優(yōu)先通過 Vuex 管理全局狀態(tài),而不是通過 this.$root 或一個全局事件總線。
28.自定義事件:始終使用 kebab-case 的事件名
?
轉(zhuǎn)載于:https://www.cnblogs.com/LUA123/p/10326881.html
總結(jié)
以上是生活随笔為你收集整理的Vue(五)Vue规范的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Redisson分布式锁分析
- 下一篇: C# WCF初识