- 功能:可以把多个组件共用的配置提取成一个混入对象,不需要在每个组件中都写了
【一】定义mixin
const hunru={
data(){
return {
title:"首页"
}
},
methods:{
handleChange(){
this.title=this.title+'_NB'
}
}
}
export default hunru
【二】在组件中局部使用
- data 中方的是当前的那么页面中默认的就是当前的
<template>
<div class="home">
<h1>{{ title }}</h1>
<button @click="handleChange">点我变标题</button>
</div>
</template>
<script>
import hunru from '@/mixin'
export default {
name: 'AboutView',
data () {
return {}
},
methods: {},
mixins: [hunru] # 如果用多个,数组继续往后拼
}
</script>
【三】在全局中使用
Vue.mixin(h)