最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

vue?mixins代碼復(fù)用的項(xiàng)目實(shí)踐

 更新時(shí)間:2022年05月24日 15:37:14   作者:鬼谷中妖  
本文主要介紹了vue?mixins代碼復(fù)用的項(xiàng)目實(shí)踐,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

導(dǎo)語:

兩年前來到新公司,開始使用vue開發(fā),代碼復(fù)用程度比較低。到后期大量的開發(fā)經(jīng)驗(yàn),以及看了一些設(shè)計(jì)模式類的書籍。才開始慢慢總結(jié)一些代碼復(fù)用的經(jīng)驗(yàn)。分享出來,

PS: Vue版本2.6

場景:

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)文章

最新評(píng)論

禄丰县| 六安市| 嵊泗县| 榆社县| 深水埗区| 温泉县| 禄劝| 罗定市| 碌曲县| 马公市| 海城市| 亳州市| 安化县| 开江县| 通辽市| 凤山市| 毕节市| 台北县| 博白县| 洛阳市| 建湖县| 浦城县| 新乡县| 衡东县| 平泉县| 萨迦县| 镶黄旗| 晴隆县| 上栗县| 萨迦县| 珠海市| 富锦市| 壤塘县| 靖西县| 晋中市| 武宣县| 鄂伦春自治旗| 尉犁县| 天水市| 合山市| 彭阳县|