Vue的Mixins详解及用法

mixins 选项接收一个混入对象的数组。这些混入对象可以像正常的实例对象一样包含实例选项,这些选项将会被合并到最终的选项中,使用的是和 Vue.extend() 一样的选项合并逻辑。也就是说,如果你的混入包含一个 created 钩子,而创建组件本身也有一个,那么两个函数都会被调用。

Mixin 钩子按照传入顺序依次调用,并在调用组件自身的钩子之前被调用。

如果发生合并冲突,则以组件对象为主。

1.使用场景

多个业务组件的数据或者逻辑相似的时候,可以将公共部分抽取。在各个业务组件中通过 mixins 引入。

2.语法

示例代码用了vue-property-decorator:

/**
* 示例代码抽取的公共部分也是class的形式(由于采用了vue-property-decorator,其提供的Mixins接收参数类型为VueClass)
* 抽取的的公共部分本质也是Vue实例,所以可以拥有常规的选项
*/
import { Vue, Component } from "vue-property-decorator";

@Component({})
export class mixins extends Vue {
  private name = 'yuzhiyong';
  beforeCreate() {
    console.log('c mixins beforeCreatd...');
  }
  created() {
    console.log('c mixins created...');
  }
  mounted() {
    console.log('c mixins mounted...');
  }
  get props():string {
    return "computed...."
  }
  sayHello():string {
    return 'mixins hello...';
  }
}

在业务组件中混入:

// 方式1:使用vue-property-decorator提供的Mixins(),直接继承
// 注意:vue-property-decorator完全依赖vue-class-component,Mixins()本质也是由vue-class-component提供的
<template>
  <div>
   <h2>Mixins</h2>
  </div>
</template>
<script lang="ts">
import { Vue, Prop, Mixins, Component } from "vue-property-decorator";
import { mixins } from "../mixins/mixins-m";

@Component({})
export default class NAME extends Mixins(mixins) {
  // name = "component props"
  created () {
    console.log(this.sayHello());
    console.log('main component created...');
  }
  mounted () {
    console.log('main component mounted...');
  }
  sayHello():string {
    return "component methods..."
  }
}
</script>

// 方式2:在@Component装饰器中使用常规的mixins选项
<template>
  <div>
   <h2>Mixins</h2>
  </div>
</template>
<script lang="ts">
import { Vue, Prop, Component } from "vue-property-decorator";
import { mixins } from "../mixins/mixins-m";

@Component({
  mixins:[mixins]
})
export default class NAME extends Vue {
  // name = "component props"
  created () {
    console.log(this.sayHello());
    console.log('main component created...');
  }
  mounted () {
    console.log('main component mounted...');
  }
  sayHello():string {
    return "component methods..."
  }
}
</script>

3.混入合并(浅合并)

混入组件和业务组件中created、mounted是钩子函数,components、methods 、computed、data是值对象。

如果值对象属性冲突时(即属性同名),业务组件选项优先 。

钩子函数内的代码,mixins和业务组件中的都会被执行,且mixins中的会被先执行。

4.全局混入

import Vue from 'vue'

Vue.mixin({
  private name = 'yuzhiyong';
  beforeCreate() {
    console.log('global beforeCreatd...');
  }
  created() {
    console.log('global created...');
  }
  mounted() {
    console.log('global mounted...');
  }
  get props():string {
    return "computed...."
  }
  sayHello():string {
    return 'mixins hello...';
  }
})


注:全局混入慎用,会影响所有创建的Vue实例。

使用Vue mixins的方法如下: 1. 在src目录下创建一个mixins文件夹,并在该文件夹下新建一个myMixins.js文件。 2. 在myMixins.js文件中,定义一个对象,并在该对象中定义你想要在组件中复用的功能选项,如data、components、methods、created、computed等。 3. 使用export导出该对象。 4. 在需要使用mixins的组件中,通过import引入myMixins.js文件,并在组件的mixins选项中添加该对象。 5. 在组件中可以直接使用mixins对象中定义的功能选项,这样可以提高代码的重用性,并使代码保持干净和易于维护。 示例代码如下: ```javascript // 在myMixins.js文件中定义mixins对象 export const myMixins = { data() { return { // 定义共用的data属性 } }, methods: { // 定义共用的方法 }, created() { // 定义共用的created钩子函数 }, // 其他共用的功能选项 } // 在需要使用mixins的组件中引入并使用mixins对象 import { myMixins } from "@/mixins/myMixins.js"; export default { mixins: [myMixins], // 组件的其他选项 } ``` 通过以上步骤,你可以轻松地在Vue组件中使用mixins,从而实现代码的重用和提高开发效率。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [Vue详解mixins混入使用](https://blog.csdn.net/qq_42198495/article/details/118424355)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小于___

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值