1. 程式人生 > 其它 >vue 例項順序

vue 例項順序

在Vue中,export default物件中有很多約定的API Key。每個人的順序排放都可能不一致,但保持統一的程式碼風格有助於團隊成員多人協作。

Vue官網文件中也有推薦順序 (opens new window),文件中對選項順序做了許多分類。但從工程專案角度思考,需要更加精簡以及合理的排序。推薦如下規則進行排序:

  1. Vue擴充套件: extends, mixins, components

  2. Vue資料: props, model, data, computed, watch

  3. Vue資源: filters, directives

  4. Vue生命週期: created, mounted, destroy...

  5. Vue方法: methods

以下推薦順序,基於團隊小夥伴@akimoto整理的順序:

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
}