目前我们是使用组件化的方式在开发整个Vue的应用程序,但是组件和组件之间有时候会存在相同的代码逻辑,我们希望对相同的代码逻辑进行抽取。
Mixin提供了一种非常灵活的方式,来分发Vue组件中的可复用功能;
<template>
<view class="content">
{{name}}
<button @tap='hello'>hello</button>
</view>
</template>
<script>
//局部引入bluetooth.js
import {bluetooth} from '../../mixins/bluetooth.js'
export default {
mixins:[bluetooth],
data() {
return {
}
},
onLoad() {
},
methods: {
}
}
</script>
<style>
</style>
新建一个bluetooth.js
export const bluetooth= {
data(){
return {
name: 'mixinTitle'
}
},
created(){
console.log('created执行')
},
onShow(){
console.log('onShow执行')
},
methods: {
hello(){
console.log('hello from mixin!');
}
}
}