Vue中mixins的使用方法详解

vue中mixins的使用方法

官方解释:

混入 (mixins): 是一种分发 Vue 组件中可复用功能的非常灵活的方式。混入对象可以包含任意组件选项。当组件使用混入对象时,所有混入对象的选项将被混入该组件本身的选项。

举个栗子:

定义一个混入对象:

export default {
    data(){
        return{
            num:1,
        }
    },
    created() {
        this.hello()
    },
    methods: {
        hello(){
            console.log('hello from mixin')
        },
    },
}

把混入对象混入到当前的组件中

<template>
  <div>
    <h1>使用mixins</h1>
    <div>组件1</div>
  </div>
</template>
​
<script>
import myMixins from "@/mixins/myMinixn";
export default {
  mixins: [myMixins],
  computed: {},
  created(){
  },
  methods: {},
};
</script>
<style>
</style>

mixins的特点:

1.方法和参数在各组件中不共享,虽然组件调用了mixins并将其属性合并到自身组件中来了,但是其属性只会被当前组件所识别并不会被共享,也就是其他组件无法从当前组件中获取到mixins中的数据和方法。

混合对象中的参数num

export default {
    data(){
        return{
            num:1,
        }
    },
    created() {
        this.hello()
    },
    methods: {
        hello(){
            console.log('hello from mixin')
        },
    },
}

组件1中的参数num进行+1的操作

<template>
  <div>
    <div>组件1里的num:{{num}}</div>
  </div>
</template>
​
<script>
import myMixins from "@/mixins/myMinixn";
export default {
  mixins: [myMixins],
  computed: {},
  created(){
    this.num++
  },
  methods: {},
};
</script>
<style>
</style>

组件2中的参数num未进行操作

<template>
  <div>
    <div>
      组件2里的num:{{num}}
    </div>
  </div>
</template>
​
<script>
import myMixins from "@/mixins/myMinixn";
export default {
  data() {
    return {};
  },
  mixins: [myMixins],
  methods: {},
};
</script>
​
<style>
</style>
​

输出结果为:组件1里的num:2

组件2里的num:1

组件1里改变了num里面的值,组件2中的num值还是混入对象里的初始值。

2.值为对象的选项,如methods,components等,选项会被合并,键冲突的组件会覆盖混入对象的

混入对象中的方法

export default {
    data(){
        return{
            num:1,
        }
    },
    created() {
        this.hello()
    },
    methods: {
        hello(){
            console.log('hello from mixin')
        },
        fun_one(){
            console.log('fun_one from mixin');
        },
        fun_two(){
            console.log('fun_two from mixin');
        }
    },
}

组件中的方法

<template>
  <div>
    <div>组件1里的num:{{num}}</div>
  </div>
</template>

<script>
import myMixins from "@/mixins/myMinixn";
export default {
  mixins: [myMixins],
  computed: {},
  created() {
    this.fun_self();
    this.fun_one();
    this.fun_two();
  },
  methods: {
    fun_self() {
      console.log("fun_self from template1");
    },
    fun_two() {
      console.log("fun_two from template1");
    },
  },
};
</script>
<style>
</style>

打印台的输出

1630649546(1).jpg

3 值为函数的选项,如created,mounted等,就会被合并调用,混合对象里的钩子函数在组件里的钩子函数之前调用

混入对象函数中的console

export default {
    data(){
        return{
            num:1,
        }
    },
    created() {
        console.log('hello from mixin');
    },
}

组件函数中的console

<template>
  <div>
    <div>组件1里的num:{{num}}</div>
  </div>
</template>

<script>
import myMixins from "@/mixins/myMinixn";
export default {
  mixins: [myMixins],
  computed: {},
  created() {
   console.log('hello from self');
  },
  methods: {
  },
};
</script>
<style>
</style>
复制代码

控制台中的打印:

image.png

与vuex的区别

vuex:用来做状态管理的,里面定义的变量在每个组件中均可以使用和修改,在任一组件中修改此变量的值之后,其他组件中此变量的值也会随之修改。

Mixins:可以定义共用的变量,在每个组件中使用,引入组件中之后,各个变量是相互独立的,值的修改在组件中不会相互影响。

与公共组件的区别

组件:在父组件中引入组件,相当于在父组件中给出一片独立的空间供子组件使用,然后根据props来传值,但本质上两者是相对独立的。

Mixins:则是在引入组件之后与组件中的对象和方法进行合并,相当于扩展了父组件的对象与方法,可以理解为形成了一个新的组件。


链接:https://juejin.cn/post/7003587726943453221
posted @ 2023-04-19 23:11  一统天下。  阅读(356)  评论(0编辑  收藏  举报