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