vue?mixins代碼復(fù)用的項(xiàng)目實(shí)踐
導(dǎo)語:
兩年前來到新公司,開始使用vue開發(fā),代碼復(fù)用程度比較低。到后期大量的開發(fā)經(jīng)驗(yàn),以及看了一些設(shè)計(jì)模式類的書籍。才開始慢慢總結(jié)一些代碼復(fù)用的經(jīng)驗(yàn)。分享出來,
PS: Vue版本2.6
場(chǎng)景:
1. 代碼里有很多當(dāng)前組件需要的純函數(shù),methods過多
<!-- 主文件 -->
<template>
<button @click="clickHandle">button</button>
</template>
<script>
export default {
name: 'PageDemo',
methods: {
func1(){},
func2(){},
func3(){},
clickHandle(){
this.func1();
this.func2()
this.func3()
console.log('button clicked')
}
},
}
</script>如果當(dāng)前組件不好拆分,就會(huì)出現(xiàn)很多函數(shù),代碼會(huì)顯得不清晰。 我現(xiàn)在的處理方法是通過mixins混入,參照MVC思想,當(dāng)前文件的的methods只寫和模板直接引用的處理方法,其他的函數(shù)都通過混入方式引用。
// compose-demo.js
export default {
methods: {
func1(){},
func2(){},
func3(){},
}
}<template>
<button @click="clickHandle">button</button>
</template>
<script>
// 主文件
import ComposeDemo from './compose-demo'
export default {
name: 'PageDemo',
mixins: [ComposeDemo],
methods: {
clickHandle(){
this.func1();
this.func2()
this.func3()
console.log('button clicked')
}
},
}
</script>充分利用mixins還有很多優(yōu)點(diǎn)。
2. 舉個(gè)例子你有一個(gè)組件需要拋出兩個(gè)數(shù)據(jù),直接的v-model不適用。需要采用$emit方法
// 組件
<template>
<input v-model="a" @change="inputChangeHandle"/>
<input v-model="b" @change="inputChangeHandle" />
</template>
<script>
export default {
name: 'ComponentChild',
props: {
propA: {
type: String
},
propB: {
type: String
}
},
data(){
return {
a: this.propA,
b: this.propB,
}
},
methods: {
inputChangeHandle(){
this.$emit('update-data', {a:this.a, b:this.b})
}
}
}
</script>
// 調(diào)用方
<template>
<component-child :propA="query.a" :propB="query.b" @update-data="getData"/>
</template>
<script>
import ComponentChild from './component-child.vue'
export default {
name: 'Page1',
components: {ComponentChild},
data(){
return {
query: {
a: '默認(rèn)數(shù)據(jù)a',
b: '默認(rèn)數(shù)據(jù)b'
}
}
},
methods: {
getData(payload) {
const {a,b}=payload;
this.query.a = a;
this.query.b = b;
}
}
}
</script>如果你有多處地方需要用到ComponentChild組件,那每個(gè)調(diào)用地方都需要寫一個(gè)方法來監(jiān)聽@update-data事件。
此時(shí),可以這樣改一下
// 純函數(shù),引入ComponentChild,并且聲明getData方法
// compose-component-child.js
<script>
import ComponentChild from './component-child.vue'
</script>
export default {
components: {ComponentChild},
methods: {
// 通常情況,復(fù)用的業(yè)務(wù)組件都會(huì)有同樣的數(shù)據(jù)結(jié)構(gòu),都帶有query.a和query.b。如果不一致,那直接在父組件重寫該方法
getData(payload) {
const {a,b}=payload;
this.query.a = a;
this.query.b = b;
}
}
}
// 調(diào)用方
<template>
<component-child :propA="query.a" :propB="query.b" @update-data="getData"/>
</template>
<script>
import ComposeComponentChild from './compose-component-child.js'
export default {
name: 'Page1',
mixins: [ComposeComponentChild]
data(){
return {
query: {
a: '默認(rèn)數(shù)據(jù)a',
b: '默認(rèn)數(shù)據(jù)b'
}
}
},
methods: { }
}
</script>借鑒了Angular的依賴注入,Page不直接聲明、引用Component,而是通過混入Compose直接使用。
Component組件,Compose引入Component并且注冊(cè)Component(聲明額外的方法),Page調(diào)用組件混入Compose,就可以可以直接使用Component組件
3. 同理,可以通過這個(gè)方式復(fù)用很多data數(shù)據(jù),避免模板化的聲明
比如常用的表格需要一下數(shù)據(jù)
<script>
import {defaultPageSize}from '@/setting'
export default {
data(){
return {
tableList: [],
pageSize: defaultPageSize,
pageNo: 1,
totalRecords: 0,
}
}
}
</script>以上數(shù)據(jù)都可以組裝為一個(gè)compose-table.js文件混入到你要使用的地方,當(dāng)然也可以通過在compose-table引用注冊(cè)表格組件。
總結(jié):
- 優(yōu)點(diǎn):提高代碼復(fù)用性,同一個(gè)組件也可以進(jìn)行更細(xì)致的功能劃分
- 缺點(diǎn):mixins無法自動(dòng)利用通過編輯器自動(dòng)導(dǎo)航到實(shí)現(xiàn)的文件,需要全項(xiàng)目搜索,對(duì)于熟悉的人來說,使用很方便。對(duì)于新人來講,閱讀代碼會(huì)有些困難。
到此這篇關(guān)于vue mixins代碼復(fù)用的項(xiàng)目實(shí)踐的文章就介紹到這了,更多相關(guān)vue mixins代碼復(fù)用內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
VUE對(duì)Storage的過期時(shí)間設(shè)置,及增刪改查方式
這篇文章主要介紹了VUE對(duì)Storage的過期時(shí)間設(shè)置,及增刪改查方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-02-02
element-ui upload組件上傳文件類型限制問題小結(jié)
最近我遇到這樣的問題,接受類型已經(jīng)加了accept 但是當(dāng)選擇彈出本地選擇文件時(shí)候切換到所有文件 之前的文件類型就本根過濾不掉了,下面小編給大家介紹element-ui upload組件上傳文件類型限制問題小結(jié),感興趣的朋友一起看看吧2024-02-02
Vue3的ts報(bào)錯(cuò):類型"{}"上不存在屬性"xxx"的兩種徹底根治解決方法
這篇文章主要給大家介紹了關(guān)于Vue3的ts報(bào)錯(cuò):類型"{}"上不存在屬性"xxx"的兩種徹底根治解決方法,這是最近做項(xiàng)目中遇到的一個(gè)問題,這里給大家總結(jié)下解決辦法,需要的朋友可以參考下2023-08-08
Vue中import from@符號(hào)指的是什么意思
這篇文章主要介紹了Vue中import from@符號(hào)指的是意思,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-03-03
VUE +Element 實(shí)現(xiàn)多個(gè)字段值拼接功能
這篇文章主要介紹了VUE +Element 實(shí)現(xiàn)多個(gè)字段值拼接,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-04-04
vue實(shí)現(xiàn)tab標(biāo)簽(標(biāo)簽超出自動(dòng)滾動(dòng))
當(dāng)創(chuàng)建的tab標(biāo)簽超出頁面可視區(qū)域時(shí)自動(dòng)滾動(dòng)一個(gè)tab標(biāo)簽距離,并可手動(dòng)點(diǎn)擊滾動(dòng)tab標(biāo)簽,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-05-05
對(duì)Vue- 動(dòng)態(tài)元素屬性及v-bind和v-model的區(qū)別詳解
今天小編就為大家分享一篇對(duì)Vue- 動(dòng)態(tài)元素屬性及v-bind和v-model的區(qū)別詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-08-08

