詳解Vue的mixin策略
我之前一直以為mixin的合并是以組件內(nèi)的優(yōu)先,即mixin的內(nèi)容如果和組件內(nèi)有沖突的,以組件內(nèi)為準(zhǔn),確實(shí)存在這種情況,但是vue指定的策略更詳細(xì),下面分別記錄各種情況對(duì)應(yīng)的合并策略
基本
當(dāng)一個(gè)組件使用mixin的時(shí)候,所有mixin的選項(xiàng)會(huì)被混入到組件自己的選項(xiàng)中, 這部分沒什么好說的,直接看代碼
// define a mixin object
const myMixin = {
created() {
this.hello()
},
methods: {
hello() {
console.log('hello from mixin!')
}
}
}
// define an app that uses this mixin
const app = Vue.createApp({
mixins: [myMixin]
})
app.mount('#mixins-basic') // => "hello from mixin!"
選項(xiàng)的合并策略
這里的選項(xiàng)指的就是 data methods和生命周期鉤子函數(shù)這些選項(xiàng),他們的會(huì)采取不同的合并策略
像data,methods,components,directives這樣的會(huì)被合并進(jìn)同一個(gè)對(duì)象中,并且遇到?jīng)_突項(xiàng)以組件的為準(zhǔn)
const myMixin = {
data() {
return {
message: 'hello',
foo: 'abc'
}
}
}
const app = Vue.createApp({
mixins: [myMixin],
data() {
return {
message: 'goodbye',
bar: 'def'
}
},
created() {
console.log(this.$data) // => { message: "goodbye", foo: "abc", bar: "def" }
}
})
const myMixin = {
methods: {
foo() {
console.log('foo')
},
conflicting() {
console.log('from mixin')
}
}
}
const app = Vue.createApp({
mixins: [myMixin],
methods: {
bar() {
console.log('bar')
},
conflicting() {
console.log('from self')
}
}
})
const vm = app.mount('#mixins-basic')
vm.foo() // => "foo"
vm.bar() // => "bar"
vm.conflicting() // => "from self"
而對(duì)于鉤子函數(shù)就不是簡(jiǎn)單的替換了,如果有同名的,他們會(huì)被一起合并進(jìn)數(shù)組中,然后依次調(diào)用,且mixin的鉤子函數(shù)會(huì)率先被調(diào)用
const myMixin = {
created() {
console.log('mixin hook called')
}
}
const app = Vue.createApp({
mixins: [myMixin],
created() {
console.log('component hook called')
}
})
// => "mixin hook called"
// => "component hook called"
全局混入和自定義選項(xiàng)
const app = Vue.createApp({
myOption: 'hello!'
})
// inject a handler for `myOption` custom option
app.mixin({
created() {
const myOption = this.$options.myOption
if (myOption) {
console.log(myOption)
}
}
})
app.mount('#mixins-global') // => "hello!"
上述代碼,我們?cè)谌謩?chuàng)建了一個(gè)自定義選項(xiàng),然后進(jìn)行了全局混入處理,但是需要注意的是,這會(huì)影響到這個(gè)app所有的子組件:
const app = Vue.createApp({
myOption: 'hello!'
})
// inject a handler for `myOption` custom option
app.mixin({
created() {
const myOption = this.$options.myOption
if (myOption) {
console.log(myOption)
}
}
})
// add myOption also to child component
app.component('test-component', {
myOption: 'hello from component!'
})
app.mount('#mixins-global')
// => "hello!"
// => "hello from component!"
我們可以看到,對(duì)于自定義選項(xiàng)這不是簡(jiǎn)單的替換,而是分別調(diào)用,當(dāng)然我們也可以制定我們自己的合并策略:
const app = Vue.createApp({})
app.config.optionMergeStrategies.customOption = (toVal, fromVal) => {
// return mergedVal
}
合并策略接收兩個(gè)參數(shù),分別是指定項(xiàng)在父實(shí)例和子實(shí)例的值,當(dāng)使用mixin的時(shí)候我們可以查看打印什么:
const app = Vue.createApp({
custom: 'hello!'
})
app.config.optionMergeStrategies.custom = (toVal, fromVal) => {
console.log(fromVal, toVal)
// => "goodbye!", undefined
// => "hello", "goodbye!"
return fromVal || toVal
}
app.mixin({
custom: 'goodbye!',
created() {
console.log(this.$options.custom) // => "hello!"
}
})
可以看到第一次從mixin打印,然后從app打印。
注意事項(xiàng)
- mixin很容易造成沖突,你得確保不會(huì)有沖突的屬性名,來避免沖突,這會(huì)造成額外的負(fù)擔(dān)
- 復(fù)用性有限,因?yàn)閙ixin不能接受參數(shù),所以邏輯是寫定的,不靈活
所以官方推薦使用 Composition Api來組織邏輯
以上就是詳解Vue的mixin策略的詳細(xì)內(nèi)容,更多關(guān)于Vue的mixin策略的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
在vue項(xiàng)目中引用Antv G2,以餅圖為例講解
這篇文章主要介紹了在vue項(xiàng)目中引用Antv G2,以餅圖為例講解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-10-10
Vue 報(bào)錯(cuò)Error: No PostCSS Config foun
這篇文章主要介紹了Vue 報(bào)錯(cuò)Error: No PostCSS Config found問題及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-07-07
npm安裝vue腳手架報(bào)錯(cuò)警告npm WARN deprecated
安裝vue腳手架報(bào)錯(cuò)可能具體原因比較多,可以根據(jù)報(bào)錯(cuò)信息進(jìn)行排查,本文主要介紹了npm安裝vue腳手架報(bào)錯(cuò)警告npm WARN deprecated,感興趣的可以了解一下2023-11-11
vue使用動(dòng)態(tài)添加路由(router.addRoutes)加載權(quán)限側(cè)邊欄的方式
這篇文章主要介紹了vue使用動(dòng)態(tài)添加路由(router.addRoutes)加載權(quán)限側(cè)邊欄的方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-06-06
vue-element-admin如何轉(zhuǎn)換成中文
這篇文章主要介紹了vue-element-admin如何轉(zhuǎn)換成中文問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-03-03
Vue+UpLoad實(shí)現(xiàn)上傳預(yù)覽和刪除圖片的實(shí)踐
本文主要介紹了Vue+UpLoad實(shí)現(xiàn)上傳預(yù)覽和刪除圖片的實(shí)踐,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-02-02
Vue全局注冊(cè)與局部注冊(cè)兩種組件注冊(cè)的方式
本文主要介紹了Vue全局注冊(cè)與局部注冊(cè)兩種組件注冊(cè)的方式,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-07-07
vue如何將后臺(tái)返回的數(shù)字轉(zhuǎn)換成對(duì)應(yīng)的文字
這篇文章主要介紹了vue如何將后臺(tái)返回的數(shù)字轉(zhuǎn)換成對(duì)應(yīng)的文字,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-10-10

