混入
混入 (mixin) 提供了一種非常靈活的方式,來分發 Vue 組件中的可複用功能。一個混入對象可以包含任意組件選項。當組件使用混入對象時,所有混入對象的選項將被“混合”進入該組件本身的選項。
個人理解就是一個儲存重用數據和方法的儲存器,方便重用
官方demo
// 定義一個混入對象
var myMixin = {
created: function () {
this.hello()
},
methods: {
hello: function () {
console.log('hello from mixin!')
}
}
}
// 定義一個使用混入對象的組件
var Component = Vue.extend({
mixins: [myMixin]
})
var component = new Component() // => "hello from mixin!"
自己再給一個demo
現在有一個模態框和一個提示框。這些提示框和模態框除了在功能上,沒有其他共同點:它們看起來不一樣,用法不一樣,但是邏輯一樣。代碼如下:
// 模態框
const Modal = {
template: '#modal',
data() {
return {
isShowing: false
}
},
methods: {
toggleShow() {
this.isShowing = !this.isShowing;
}
},
components: {
appChild: Child
}
}
// 提示框
const Tooltip = {
template: '#tooltip',
data() {
return {
isShowing: false
}
},
methods: {
toggleShow() {
this.isShowing = !this.isShowing;
}
},
components: {
appChild: Child
}
}
//合併之後
const toggle = {
data () {
isshowing: false
},
methods: {
toggleShow() {
this.isshowing = !this.isshowing
}
}
}
const Modal = {
template: '#modal',
mixins: [toggle],
components: {
appChild: Child
}
};
const Tooltip = {
template: '#tooltip',
mixins: [toggle],
components: {
appChild: Child
}
};
開發過程中,如果你是以vue-cli創建的項目來寫,可以這樣
// mixin.js
export const toggle = {
data () {
isshowing: false
},
methods: {
toggleShow() {
this.isshowing = !this.isshowing
}
}
}
// modal.vue
// 將mixin引入該組件,就可以直接使用 toggleShow() 了
import {mixin} from '../mixin.js'
export default {
mixins: [mixin],
mounted () {
}
}
// tooltip組件同上
選項合併(重點)
1.數據對象data
數據對象在內部會進行遞歸合併,並在發生衝突時以組件數據優先。
var mixin = {
data: function () {
return {
message: 'hello',
foo: 'abc'
}
}
}
new Vue({
mixins: [mixin],
data: function () {
return {
message: 'goodbye',
bar: 'def'
}
},
created: function () {
console.log(this.$data)
// => { message: "goodbye", foo: "abc", bar: "def" }
}
})
2.鉤子函數
同名鉤子函數將合併爲一個數組,因此都將被調用。另外,混入對象的鉤子將在組件自身鉤子之前調用。
var mixin = {
created: function () {
console.log('混入對象的鉤子被調用')
}
}
new Vue({
mixins: [mixin],
created: function () {
console.log('組件鉤子被調用')
}
})
// => "混入對象的鉤子被調用"
// => "組件鉤子被調用"
3.值爲對象的選項
值爲對象的選項,例如 methods、components 和 directives,將被合併爲同一個對象。兩個對象鍵名衝突時,取組件對象的鍵值對。
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"
注意:Vue.extend() 也使用同樣的策略進行合併。
全局混入
混入也可以進行全局註冊。使用時格外小心!一旦使用全局混入,它將影響每一個之後創建的 Vue 實例。使用恰當時,這可以用來爲自定義選項注入處理邏輯。請謹慎使用全局混入,因爲它會影響每個單獨創建的 Vue 實例 (包括第三方組件)。大多數情況下,只應當應用於自定義選項,因此,它們的使用場景極其有限並且要非常的小心。一個我能想到的用途就是它像一個插件,你需要賦予它訪問所有東西的權限。但即使在這種情況下,我也對你正在做的保持警惕,尤其是你在應用中擴展的函數,可能對你來說是不可知的。
// 爲自定義的選項 'myOption' 注入一個處理器。
Vue.mixin({
created: function () {
var myOption = this.$options.myOption
if (myOption) {
console.log(myOption)
}
}
})
new Vue({
myOption: 'hello!'
})
// => "hello!"
自定義選項合併策略
這種一般不去自己弄,本人也幾乎沒有使用,還是看官方解釋吧。自定義選項將使用默認策略,即簡單地覆蓋已有值。如果想讓自定義選項以自定義邏輯合併,可以向 Vue.config.optionMergeStrategies 添加一個函數:
Vue.config.optionMergeStrategies.myOption = function (toVal, fromVal) {
// 返回合併後的值
}
對於多數值爲對象的選項,可以使用與 methods 相同的合併策略:
var strategies = Vue.config.optionMergeStrategies
strategies.myOption = strategies.methods
可以在 Vuex 1.x 的混入策略裏找到一個更高級的例子:
const merge = Vue.config.optionMergeStrategies.computed
Vue.config.optionMergeStrategies.vuex = function (toVal, fromVal) {
if (!toVal) return fromVal
if (!fromVal) return toVal
return {
getters: merge(toVal.getters, fromVal.getters),
state: merge(toVal.state, fromVal.state),
actions: merge(toVal.actions, fromVal.actions)
}
}
再看一個demo吧
import {mapGetters} from 'vuex'
// 目的是想要處理 scroll 的bottom值,在含有playlist列表的情況下
export const playlistMixin = {
computed: {
...mapGetters([
'playList'
])
},
mounted() {
this.handlePlaylist(this.playList)
},
activated() {
this.handlePlaylist(this.playList)
},
watch: {
playlist(newVal) {
this.handlePlaylist(newVal)
}
},
methods: {
// 如果組件中沒有這個方法,那麼就報錯
handlePlaylist() {
throw new Error('component must implement handlePlaylist method')
}
}
}
總的來說混合對於封裝一小段想要複用的代碼來講是有用的。對你來說它們當然不是唯一可行的。混合很好,它不需要傳遞狀態,但是這種模式當然也可能會被濫用。使用還是要合情合景。