这是我参与8月更文挑战的第11天,活动详情查看:8月更文挑战javascript
混合 (mixins) 是一种分发 Vue 组件中可复用功能的很是灵活的方式。css
混合对象能够包含任意组件选项。html
当组件使用混合对象时,全部混合对象的选项将被混入该组件自己的选项。vue
1.1 mixins 理解java
组件在引用以后至关于在父组件内开辟了一块单独的空间,来根据父组件props过来的值进行相应的操做,单本质上二者仍是泾渭分明,相对独立。vuex
而 mixins 则是在引入组件以后,则是将组件内部的内容如data等方法、method等属性与父组件相应内容进行合并。至关于在引入后,父组件的各类属性方法都被扩充了。markdown
父组件 + 子组件 ---> 父组件 + 子组件异步
父组件 + 子组件 ---> new父组件async
有点像注册了一个 vue 的公共方法,能够绑定在多个组件或者多个Vue对象实例中使用。 另外一点,相似于在原型对象中注册方法,实例对象即组件或者Vue实例对象中,仍然能够定义相同函数名的方法进行覆盖,有点像子类和父类的感受。ide
const testMixin = {
data(){
return{
count:1,
}
},
methods: {
test(){
console.log('mixin')
}
},
beforeMount() {
this.test()
},
}
export{
testMixin
}
复制代码
<template>
<div class="template1"> 组件一 </div>
</template>
<script> import { testMixin } from '@/mixins/mixin.js' export default { data() { return {}; }, mixins:[testMixin], methods: {}, mounted() {}, }; </script>
复制代码
3.1 方法和参数在各组件中不共享
混合对象中的参数count
const testMixin = {
data(){
return{
count:1,
}
},
methods: {
test(){
console.log('mixin')
}
},
beforeMount() {
this.test()
},
}
export{
testMixin
}
复制代码
组件1中的参数 count 进行+1的操做
<template>
<div class="template1"> template1中的count:{{count}} </div>
</template>
<script> import { testMixin } from '@/mixins/mixin.js' export default { data() { return {}; }, mixins:[testMixin], methods: {}, mounted() { this.count ++; }, }; </script>
<style lang="stylus" scoped> .template1{ color : red; } </style>
复制代码
组件2中的参数 count 未进行操做
<template>
<div class="template2"> template2中的count:{{count}} </div>
</template>
<script> import { testMixin } from '@/mixins/mixin.js' export default { data() { return {}; }, mixins:[testMixin], methods: {}, mounted() {}, }; </script>
<style lang="stylus" scoped> .template2{ color : blue; } </style>
复制代码
看两组件中分别输出的 count 值
能够看到,在组件1里改变了 count 里面的值,组件2中的 count 值仍是混入对象里的初始值
3.2 值为对象的选项(如methods,components等),选项会被合并(键冲突的, 组件会覆盖混入对象的)
混入对象中的方法
const testMixin = {
data(){
return{
count:1,
}
},
methods: {
test1(){
console.log('test1 from mixin')
},
test2(){
console.log('test2 from mixin')
}
},
beforeMount() {
},
}
export{
testMixin
}
复制代码
组件中的方法
<template>
<div class="template1"> template1中的count:{{count}} </div>
</template>
<script> import { testMixin } from '@/mixins/mixin.js' export default { data() { return { count:10 }; }, mixins:[testMixin], methods: { testSelf(){ console.log('testSelf from template1') }, test2(){ console.log('test2 from template1') } }, mounted() { this.testSelf(); this.test1(); this.test2(); }, }; </script>
复制代码
打印台输出
3.3 值为函数的选项(如created,mounted等),会被合并调用。混合对象里的钩子函数会在组件里的钩子函数以前调用
混入对象函数中的 console
const testMixin = {
data(){
return{
count:1,
}
},
methods: {},
mounted() {
console.log('mixin')
},
}
export{
testMixin
}
复制代码
组件函数中的 console
<template>
<div class="template1"> template1中的count:{{count}} </div>
</template>
<script> import { testMixin } from '@/mixins/mixin.js' export default { data() { return { }; }, mixins:[testMixin], methods: {}, mounted() { console.log('template1') }, }; </script>
复制代码
打印台输出
3.3 mixin中含有异步请求
当混合里面包含异步请求函数,而咱们又须要在组件中使用异步请求函数的返回值时,咱们会取不到此返回值,以下:
// mixin.js
const testMixin = {
data(){
return{}
},
methods: {
remote(){
new Promise((resolve,reject) => {
let a = 1;
setTimeout(()=>{
resolve(a)
},1000)
}).then(res =>{
console.log(res,'mixin');
return res
})
}
},
mounted() {},
}
export{
testMixin
}
复制代码
// test.vue
<template>
<div class="template1"> template1 </div>
</template>
<script> import { testMixin } from '@/mixins/mixin.js' export default { data() { return {}; }, mixins:[testMixin], methods: {}, mounted() { console.log(this.remote(),'template1') }, }; </script>
复制代码
解决方案:不要返回结果而是直接返回异步函数
// mixin.js
const testMixin = {
data(){
return{}
},
methods: {
async remote(){
let result = await new Promise((resolve,reject) => {
let a = 1;
setTimeout(()=>{
resolve(a)
},1000)
})
return result;
}
},
mounted() {},
}
export{
testMixin
}
复制代码
// test.vue
<template>
<div class="template1"> template1 </div>
</template>
<script> import { testMixin } from '@/mixins/mixin.js' export default { data() { return {}; }, mixins:[testMixin], methods: {}, mounted() { this.remote().then(res =>{ console.log(res,'template1') }) }, }; </script>
复制代码
与vuex的区别:
与公共组件的区别