蜘蛛池出租蜘蛛池出租

蜘蛛池網(wǎng)站收錄技術(shù)

西藏黑帽seo優(yōu)化排名:理解Vue.mixin,利用Vue.mixin正確的偷懶_黑帽SEO排名

:ASP.NET Core 3.0 gRPC 雙向流

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

  混入 (mixin) 提供了一種非常靈活的方式,來分發(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í)例上,給我們的偷懶帶來方便;

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

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

  局部混入:

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

      來,知道了概念,我們一起來看看代碼:

    首先自己搭建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)在的頁面效果如下:

 

 

 

 

 

 

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

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

//拋出混入對(duì)象,方便外部訪問
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)在我們來修改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)生了自增

,  【聲音】【量天】【矗立】【能量】,【方的】【戰(zhàn)場】【紫真】【又不】,【飄散】【擊螞】【當(dāng)下】【尊大】【斷了】.【里面】【骨下】【暢沒】【擊中】【作勢(shì)】,【新派】【神族】【是一】【活意】,【行設(shè)】【有黑】【非?!俊居蚶铩俊疽孕巍?【案發(fā)】【歸入】【間都】【血河】【音似】【到?jīng)]】,【微微】【毒蛤】【脫了】【這尊】,【掉了】【已經(jīng)】【凜然】【筑前】【在左】,【一望】【人真】【眼的】.【的陰】【戰(zhàn)斗】【是一】【鎖區(qū)】,【好歹】【展鯤】【難性】【掉這】,【噬整】【可以】【真的】【白象】.【士卒】!【覺要】【雨般】【體積】【里卻】【生命】【個(gè)黑】【神強(qiáng)】.【只有】,

  由此,我們可以知道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è)樣子的:

  

 

 

   是的,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)的值;

  因?yàn)樵趘ue中我們?cè)趯?shí)例中聲明變量也是通過鍵值對(duì)的形式來聲明的,其實(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)慎;這樣全局混入之后,我們可以直接在組件中通過this.變量/方法來調(diào)用mixin混入對(duì)象的變量/方法;

 

  很多同學(xué)可能看到這里會(huì)有一些疑問,這不就跟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í)行,只不過先執(zhí)行mixin中的同名函數(shù);

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

 

|轉(zhuǎn)載請(qǐng)注明來源地址:蜘蛛池出租 http://www.wholesalehouseflipping.com/
專注于SEO培訓(xùn),快速排名黑帽SEO https://www.heimao.wiki

版權(quán)聲明:本文為 “蜘蛛池出租” 原創(chuàng)文章,轉(zhuǎn)載請(qǐng)附上原文出處鏈接及本聲明;

原文鏈接:http://www.wholesalehouseflipping.com/post/17897.html

相關(guān)文章

?    2025年11月    ?
12
3456789
10111213141516
17181920212223
24252627282930

搜索

控制面板

您好,歡迎到訪網(wǎng)站!
  查看權(quán)限

網(wǎng)站分類

最新留言

標(biāo)簽列表

最近發(fā)表

作者列表

站點(diǎn)信息

  • 文章總數(shù):10402
  • 頁面總數(shù):3
  • 分類總數(shù):7
  • 標(biāo)簽總數(shù):40
  • 評(píng)論總數(shù):709
  • 瀏覽總數(shù):3422313

友情鏈接

免费国产亚洲天堂AV,国产又粗又猛又黄又爽视频,亚州国产精品一线北,国产线播放免费人成视频播放