vue?mixins代碼復(fù)用的項(xiàng)目實(shí)踐
導(dǎo)語(yǔ):
兩年前來(lái)到新公司,開始使用vue開發(fā),代碼復(fù)用程度比較低。到后期大量的開發(fā)經(jīng)驗(yàn),以及看了一些設(shè)計(jì)模式類的書籍。才開始慢慢總結(jié)一些代碼復(fù)用的經(jīng)驗(yàn)。分享出來(lái),
PS: Vue版本2.6
場(chǎng)景:
1. 代碼里有很多當(dāng)前組件需要的純函數(shù),methods過(guò)多
<!-- 主文件 --> <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)在的處理方法是通過(guò)mixins
混入,參照MVC思想,當(dāng)前文件的的methods只寫和模板直接引用的處理方法,其他的函數(shù)都通過(guò)混入方式引用。
// 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è)方法來(lái)監(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
,而是通過(guò)混入Compose
直接使用。
Component
組件,Compose
引入Component
并且注冊(cè)Component
(聲明額外的方法),Page
調(diào)用組件混入Compose
,就可以可以直接使用Component
組件
3. 同理,可以通過(guò)這個(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)然也可以通過(guò)在compose-table
引用注冊(cè)表格組件。
總結(jié):
- 優(yōu)點(diǎn):提高代碼復(fù)用性,同一個(gè)組件也可以進(jìn)行更細(xì)致的功能劃分
- 缺點(diǎn):mixins無(wú)法自動(dòng)利用通過(guò)編輯器自動(dòng)導(dǎo)航到實(shí)現(xiàn)的文件,需要全項(xiàng)目搜索,對(duì)于熟悉的人來(lái)說(shuō),使用很方便。對(duì)于新人來(lái)講,閱讀代碼會(huì)有些困難。
到此這篇關(guān)于vue mixins代碼復(fù)用的項(xiàng)目實(shí)踐的文章就介紹到這了,更多相關(guān)vue mixins代碼復(fù)用內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue兩個(gè)輸入框聯(lián)動(dòng)校驗(yàn)方式(最大值-最小值)
這篇文章主要介紹了vue兩個(gè)輸入框聯(lián)動(dòng)校驗(yàn)方式(最大值-最小值),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-10-10vue使用video插件vue-video-player的示例
這篇文章主要介紹了vue使用video插件vue-video-player的示例,幫助大家更好的理解和使用vue插件,感興趣的朋友可以了解下2020-10-10vue基礎(chǔ)之data存儲(chǔ)數(shù)據(jù)及v-for循環(huán)用法示例
這篇文章主要介紹了vue基礎(chǔ)之data存儲(chǔ)數(shù)據(jù)及v-for循環(huán)用法,結(jié)合實(shí)例形式分析了vue.js使用data存儲(chǔ)數(shù)據(jù)、讀取數(shù)據(jù)及v-for遍歷數(shù)據(jù)相關(guān)操作技巧,需要的朋友可以參考下2019-03-03深入探討Vue3實(shí)現(xiàn)多數(shù)據(jù)變化監(jiān)聽的方式
隨著 Vue 3 的發(fā)布,框架帶來(lái)了更多的新特性和增強(qiáng),其中之一就是 watch 函數(shù)的升級(jí),這一改進(jìn)使得多個(gè)數(shù)據(jù)的變化偵聽更加優(yōu)雅和靈活,本文將深入探討 Vue 3 中的 watch 函數(shù),以及如何以更加優(yōu)雅的方式實(shí)現(xiàn)對(duì)多個(gè)數(shù)據(jù)變化的監(jiān)聽2023-08-08vue封裝全局彈窗警告組件this.$message.success問(wèn)題
這篇文章主要介紹了vue封裝全局彈窗警告組件this.$message.success問(wèn)題,具有很的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-09-09vue使用數(shù)組splice方法失效,且總刪除最后一項(xiàng)的問(wèn)題及解決
這篇文章主要介紹了vue使用數(shù)組splice方法失效,且總刪除最后一項(xiàng)的問(wèn)題及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-09-09vue組件實(shí)現(xiàn)列表自動(dòng)無(wú)限循環(huán)的方法
最近剛好有個(gè)功能需要實(shí)現(xiàn)列表的無(wú)限循環(huán)滾動(dòng),這篇文章主要給大家介紹了關(guān)于vue組件實(shí)現(xiàn)列表自動(dòng)無(wú)限循環(huán)的相關(guān)資料,文中通過(guò)代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-11-11Vue封裝一個(gè)TodoList的案例與瀏覽器本地緩存的應(yīng)用實(shí)現(xiàn)
這篇文章主要介紹了Vue封裝一個(gè)TodoList的案例與瀏覽器本地緩存的應(yīng)用實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-04-04