官網(wǎng):混合 (mixins) 是一種分發(fā) Vue 組件中可復用功能的非常靈活的方式。混合對象可以包含任意組件選項。以組件使用混合對象時,所有混合對象的選項將被混入該組件本身的選項。
個人:混入就是用來對vue組件中的公共部分,包括數(shù)據(jù)對象、鉤子函數(shù)、方法等所有選項,進行提取封裝,以達到代碼的復用,混合用處挺大的,然我們來看看實際用法。
基礎(chǔ)用法
// 這是在main.js根文件中使用,在組中使用也是一樣 import Vue from 'vue'; var mixin = { data() { return { name: 'www.vipbic.com', author: '羊先生' } }, created: function() { console.log('Website:' + this.name) }, methods: { foo: function() { console.log('作者:' + this.author) }, conflicting: function() { console.log('from mixin') } } } new Vue({ mixins: [mixin], render: h => h(App), created() { let option = this.$options.doNotInit console.log('option:', ); this.foo() } }).$mount('#app') // 在組建中使用