- Vue mixins的优点,可以将不同的组件,相同的逻辑,简化整理。
<div>
<h1 v-if="show">Hello 我是Component</h1>
<button @click="handleAvg">切换</button>
</div>
- 新建Mixins.js,将data return以及methos复写到Mixins中,导出函数名称showMix
export const showMix = {
data() {
return {
show: true
};
},
methods: {
handleAvg() {
this.show = !this.show;
}
}
};
- 如何应用
3.1导入刚才新建的Mixins中
<script>
import {showMix} from './components/Mixins'
export default {
mixins:[showMix]
}
</script>