Vue 混合

混合(mixins)是一种分发vue组件中可复用功能的非常灵活的方式。混合对象可以可以包含任意组件选项。以组件使用混合对象时,所有混合对象的选项将被混合到该组件本身的选项。

//定义一个混合对象

var myMixins = {

created:function(){

this.hello();

},

methods:{

hello:function(){

console.log('hello from mixin!');

}

}

}

//定义一个使用混合对象的组件

var Component = vue.extend({

mixins:[myMIxins]

});

 

var component = new Component();// -->hello from mixin!

 

选项合并

 

当组件和混合对象含有同名选项时,这些选项将以恰当的方式混合。比如,同名钩子函数将混合为一个数组,因此都将被调用。

混合对象的钩子将在组件自身钩子之前调用。

var mixin = {
created: function () {
console.log('混合对象的钩子被调用')
}
}
 
new Vue({
mixins: [mixin],
created: function () {
console.log('组件钩子被调用')
}
})

值为对象的选项,例如methods,component ,和directives,将被混合为同一个对象,两个对象键名冲突时,取组件对象的键值对。

var mixin = {

methods:function(){

console.log('foo')

}

conflicting:function(){

console.log('mixin')

}

}

var vm = new Vue({

mixins:[mixin],

methods:{

bar:function(){

console.log('bar')

},

conflicting:function(){

console.log('from self')

}

}

})

vm.foo();

vm.bar();

vm.conflicting();

 

posted @ 2017-08-25 18:12  susanws  阅读(550)  评论(0编辑  收藏  举报