混入mixin
概念
mixin 混入:可以把多个组件共用的配置提取成一个混入对象
提取混入对象
export const hunhe={
methods:{
show(){
console.log('show');
}
},
data(){
return{
age:12
}
}
}
注册混入,全局注册(只要注册了,所有组件都可以用)
import {hunhe} from './mixin'
const app = createApp(App)
// 全局引用mixin
app.mixin(hunhe)
app.mount('#app')
注册混入,局部注册,在组件中注册了,这个组件可以用(在组件中)
<script>
import { hunhe } from '../mixin';
export default{
mixins:[hunhe]
}
</script>
<template>
<div>
<button @click="show">{{age}}</button>
</div>
</template>