mixins: 混合對(duì)象, 功能類(lèi)似于Vue.extend();
使用時(shí),在組件的選項(xiàng)中添加mixins: [mixin]
var mixin = {
methods: {
foo: function () {
console.log('foo')
},
conflicting: function () {
console.log('from mixin')
}
}
}
var vm = new Vue({
mixins: [mixin],
methods: {
bar: function () {
console.log('bar')
},
conflicting: function () {
console.log('from self')
}
}
})
vm.foo() // => "foo"
vm.bar() // => "bar"
vm.conflicting() // => "from self"
注意事項(xiàng):
1-minxins可以包含任意組件對(duì)象的選項(xiàng)(例如:methods, mounted等鉤子函數(shù), components, directives等等),
2-當(dāng)組件使用minxins對(duì)象時(shí), minxins對(duì)象的選項(xiàng), 將被混合進(jìn)組件的選項(xiàng)
3-如果minxins對(duì)象的選項(xiàng)是鉤子函數(shù)(created, mounted等)怎會(huì)先于組件的鉤子函數(shù)執(zhí)行
4-如果minxins對(duì)象的選項(xiàng)為對(duì)象(如methods, directives,components), 則會(huì)合并為同意對(duì)象, 如果鍵值發(fā)生沖突, 則會(huì)使用組件的鍵值對(duì)