vue的mixins(個人筆記)

參考鏈接https://www.cnblogs.com/luguankun/p/10849636.html

https://www.jianshu.com/p/19705ef09354

 

mixins就是定義一部分公共的方法或者計算屬性,然後混入到各個組件中使用,方便管理與統一修改

什麼時候使用Mixins?

頁面的風格不用,但是執行的方法和需要的數據類似

 

同一個生命週期,mixins對象會比組件的先執行。

創建一個js文件作爲mixins

在同一個mixins文件中,可以存在多個mixins對象

export const mixinsTest = {
    methods:{
        hello(){
            console.log("hello");
            
        }
    },
    created(){
        this.hello()
    }
}
export const mixinsTest2 = {
    methods:{
        hello2(){
            console.log("hello2");
        }
    },
    created() {
        this.hello2();
    },
}

在其他組件中引入這個mixins

import {mixinsTest,mixinsTest2} from '../util/test.js'

引用和使用

<template>
<div>
    home
</div>
</template>
<script>
import {mixinsTest,mixinsTest2} from '../util/test.js'
export default {
  name: "Home",
  data () {
    return {
    };
  },
  created(){
      console.log("1212");
  },
  mixins:[mixinsTest2,mixinsTest] // 先調用那個mixins對象,就先執行哪個
 
}
</script>
<style lang="css" scoped>
</style>

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章