理解Vue.mixin,利用Vue.mixin正確的偷懶

關(guān)于Vue.mixin在vue官方文檔中是這么解釋的:

混入 (mixin) 提供了一種非常靈活的方式,來(lái)分發(fā) Vue 組件中的可復(fù)用功能。一個(gè)混入對(duì)象可以包含任意組件選項(xiàng)。當(dāng)組件使用混入對(duì)象時(shí),所有混入對(duì)象的選項(xiàng)將被“混合”進(jìn)入該組件本身的選項(xiàng)。

我們的理解:Vue.mixin給我們提供了一種混入Vue實(shí)例的方法,創(chuàng)建了混入對(duì)象之后,我們自定義的方法或者變量可以很輕松的掛載在Vue實(shí)例上,給我們的偷懶帶來(lái)方便;

Vue.mixin為我們提供了兩種混入方式:局部混入和全局混入;

本文還是以demo形式來(lái)進(jìn)行學(xué)習(xí)講解,如果有條件最好還是跟著demo敲一遍,這樣印象才會(huì)深刻;

局部混入:

顧名思義就是部分混入,也就是只有引入了mixin的混入對(duì)象才可以使用,并且只有在引入了mixin混入對(duì)象的組件中才生效;

來(lái),知道了概念,我們一起來(lái)看看代碼:

首先自己搭建Vue的開發(fā)環(huán)境,然后我們?cè)趕rc目錄中新建兩個(gè)vue文件,分別是page1.vue和page2.vue;

page1.vue

<template>
    <div>page1的值是:</div>
</template>

<script>
export default {
  data () {
    return {
     
    }
  },
}
</script>

<style scoped>

</style>

page2.vue

<template>
    <div>page2的值是:</div>
</template>

<script>
export default {
  data () {
    return {
        
    }
  }
}
</script>

<style scoped>

</style>

然后我們修改App.vue

<template>
  <div id="app">
    <button @click="method1">page1</button>
    <button @click="method2">page2</button>

    <router-view></router-view>
  </div>
</template>

<script>
export default {
  name: 'App',
  methods:{
    method1(){
      this.$router.push('/page1');
    },
    method2(){
      this.$router.push('/page2');
    }
  }
}
</script>

<style>
#app {
  font-family: 'Avenir', Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-align: center;
  color: #2c3e50;
  margin-top: 60px;
}
</style>

在src目錄下創(chuàng)建router.js文件,配置路由實(shí)現(xiàn)跳轉(zhuǎn)

import Vue from "vue";
import VueRouter from "vue-router";
Vue.use(VueRouter);

import page1 from "./page1";
import page2 from "./page2";

const routes=[
    {path:"/page1",component:page1},
    {path:"/page2",component:page2}
]


const router=new VueRouter({
    routes
})


export default router

最后將路由引入main.js中:

// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import App from './App'
import router from './router.js'

Vue.config.productionTip = false


/* eslint-disable no-new */
new Vue({
  el: '#app',
  router,
  components: { App },
  template: '<App/>'
})

完成上述準(zhǔn)備工作之后,我們可以看到現(xiàn)在的頁(yè)面效果如下:

image.png

沒(méi)有報(bào)錯(cuò),我們開始正式進(jìn)入學(xué)習(xí)Vue.mixin:

首先我們?cè)趕rc目錄下新建一個(gè)名為mixin的文件夾并在mixin文件中創(chuàng)建一個(gè)mixin.js文件:

//拋出混入對(duì)象,方便外部訪問(wèn)
export const mixin={
    data(){
        return {
            number:1
        }
    }
}

可以看到我們?cè)诨烊雽?duì)象中創(chuàng)建了一個(gè)變量,是的,混入對(duì)象跟Vue實(shí)例的格式是一樣的;

然后我們可以將mixin.js引入到我們的page1.vue和page2.vue中

page1.vue

<template>
    //這里讀的值其實(shí)是mixin的值,因?yàn)檫@個(gè)時(shí)候mixin已經(jīng)混入到vue實(shí)例中了
    <div>page1的值是:{{number}}</div>
</template>

<script>
//引入mixin.js
import {mixin} from "./mixin/mixin"
export default {
//這里注意:屬性名為mixins,值為數(shù)組類型
  mixins:[mixin],
  data () {
    return {
     
    }
  },
}
</script>

<style scoped>

</style>

page2.vue

<template>
    <div>page2的值是:{{number}}</div>
</template>

<script>
import {mixin} from "./mixin/mixin"
export default {
  mixins:[mixin],
  data () {
    return {
        
    }
  }
}
</script>

<style scoped>

</style>

這個(gè)時(shí)候我們的混入對(duì)象已經(jīng)成功混入到Vue實(shí)例中,你們可以點(diǎn)擊看看效果,是可以正常運(yùn)行并且能讀取到值的;

現(xiàn)在我們來(lái)修改page1.vue的代碼:

<template>
    <div>page2的值是:{{number}}</div>
</template>

<script>
import {mixin} from "./mixin/mixin"
export default {
  mixins:[mixin],
  data () {
    return {
        
    }
  }
}
</script>

<style scoped>

</style>

page2不變,再運(yùn)行可以發(fā)現(xiàn),我們的page1.vue中的值是執(zhí)行了mounted,所以產(chǎn)生了自增

由此,我們可以知道m(xù)ixin混入對(duì)象的變量是不會(huì)共享的;也就是你page1發(fā)生了變化,并不會(huì)通知mixin進(jìn)行實(shí)時(shí)刷新數(shù)據(jù),發(fā)生的變化只會(huì)在page1.vue中生效,不影響其他組件;

現(xiàn)在我們修改mixin.js和page1.vue中的代碼:

mixin.js

export const mixin={
    data(){
        return {
            number:1
        }
    },
    created(){
            console.log("mixin混入對(duì)象")
    }
}

page1.vue

<template>
    <div>page1的值是:{{number}}</div>
</template>

<script>
import {mixin} from "./mixin/mixin"
export default {
  mixins:[mixin],
  data () {
    return {
     
    }
  },
  created(){
          console.log("這里是page1");
  }
}
</script>

<style scoped>

</style>

這個(gè)時(shí)候我們?cè)龠\(yùn)行可以發(fā)現(xiàn)控制臺(tái)輸出是這個(gè)樣子的:

image

是的,mixin混入對(duì)象中聲明了:如果是同名鉤子函數(shù)將合并為一個(gè)數(shù)組,因此都被調(diào)用,但是混入對(duì)象的鉤子將在自身實(shí)例鉤子之前觸發(fā);

值為對(duì)象的選項(xiàng),例如methods,components等如果變量名和mixin混入對(duì)象的變量名發(fā)生沖突,將會(huì)以組件優(yōu)先并進(jìn)行遞歸合并,相當(dāng)于組件數(shù)據(jù)直接覆蓋了mixin中的同名數(shù)據(jù);

我們可以修改代碼mixin.js和page1.vue

mixin.js

export const mixin={
    data(){
        return {
            number:1
        }
    },
    methods:{
        demo1(){
            console.log("mixin混入對(duì)象")
        }
    }
}

page1.vue

<template>
    <div>page1的值是:{{number}}</div>
</template>

<script>
import {mixin} from "./mixin/mixin"
export default {
  mixins:[mixin],
  data () {
    return {
        number:10
    }
  },
  mounted(){
      this.demo1();
  },
  methods:{
      demo1(){
        console.log("這里是page1");
      }   
  }
}
</script>

<style scoped>

</style>

運(yùn)行代碼我們可以很清晰的看到都是執(zhí)行我們組件內(nèi)的值;

image

因?yàn)樵趘ue中我們?cè)趯?shí)例中聲明變量也是通過(guò)鍵值對(duì)的形式來(lái)聲明的,其實(shí)也是一個(gè)對(duì)象;

全局混入:

全局混入我們只需要把mixin.js引入到main.js中,然后將mixin放入到Vue.mixin()方法中即可;

// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import App from './App'
import router from './router.js'
import mixin from "./mixin/mixin.js"
Vue.config.productionTip = false
Vue.mixin(mixin)

/* eslint-disable no-new */
new Vue({
  el: '#app',
  router,
  components: { App },
  template: '<App/>'
})

是的,全局混入更為便捷,我們將不用在子組件聲明,全局混入將會(huì)影響每一個(gè)組件的實(shí)例,使用的時(shí)候需要小心謹(jǐn)慎;這樣全局混入之后,我們可以直接在組件中通過(guò)this.變量/方法來(lái)調(diào)用mixin混入對(duì)象的變量/方法;

很多同學(xué)可能看到這里會(huì)有一些疑問(wèn),這不就跟Vuex差不多嘛,其實(shí)不是的:

mixin混入對(duì)象和Vuex的區(qū)別:

Vuex是狀態(tài)共享管理,所以Vuex中的所有變量和方法都是可以讀取和更改并相互影響的;

mixin可以定義公用的變量或方法,但是mixin中的數(shù)據(jù)是不共享的,也就是每個(gè)組件中的mixin實(shí)例都是不一樣的,都是單獨(dú)存在的個(gè)體,不存在相互影響的;

mixin混入對(duì)象值為函數(shù)的同名函數(shù)選項(xiàng)將會(huì)進(jìn)行遞歸合并為數(shù)組,兩個(gè)函數(shù)都會(huì)執(zhí)行,只不過(guò)先執(zhí)行mixin中的同名函數(shù);

mixin混入對(duì)象值為對(duì)象的同名對(duì)象將會(huì)進(jìn)行替換,都優(yōu)先執(zhí)行組件內(nèi)的同名對(duì)象,也就是組件內(nèi)的同名對(duì)象將mixin混入對(duì)象的同名對(duì)象進(jìn)行覆蓋;

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容