午夜剧场伦理_日本一道高清_国产又黄又硬_91黄色网战_女同久久另类69精品国产_妹妹的朋友在线

您的位置:首頁技術文章
文章詳情頁

詳解Vue的mixin策略

瀏覽:136日期:2022-10-25 09:42:54

我之前一直以為mixin的合并是以組件內的優先,即mixin的內容如果和組件內有沖突的,以組件內為準,確實存在這種情況,但是vue指定的策略更詳細,下面分別記錄各種情況對應的合并策略

基本

當一個組件使用mixin的時候,所有mixin的選項會被混入到組件自己的選項中, 這部分沒什么好說的,直接看代碼

// define a mixin objectconst myMixin = { created() { this.hello() }, methods: { hello() { console.log(’hello from mixin!’) } }}// define an app that uses this mixinconst app = Vue.createApp({ mixins: [myMixin]})app.mount(’#mixins-basic’) // => 'hello from mixin!'

選項的合并策略

這里的選項指的就是 data methods和生命周期鉤子函數這些選項,他們的會采取不同的合并策略

像data,methods,components,directives這樣的會被合并進同一個對象中,并且遇到沖突項以組件的為準

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'

而對于鉤子函數就不是簡單的替換了,如果有同名的,他們會被一起合并進數組中,然后依次調用,且mixin的鉤子函數會率先被調用

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'

全局混入和自定義選項

const app = Vue.createApp({ myOption: ’hello!’})// inject a handler for `myOption` custom optionapp.mixin({ created() { const myOption = this.$options.myOption if (myOption) { console.log(myOption) } }})app.mount(’#mixins-global’) // => 'hello!'

上述代碼,我們在全局創建了一個自定義選項,然后進行了全局混入處理,但是需要注意的是,這會影響到這個app所有的子組件:

const app = Vue.createApp({ myOption: ’hello!’})// inject a handler for `myOption` custom optionapp.mixin({ created() { const myOption = this.$options.myOption if (myOption) { console.log(myOption) } }})// add myOption also to child componentapp.component(’test-component’, { myOption: ’hello from component!’})app.mount(’#mixins-global’)// => 'hello!'// => 'hello from component!'

我們可以看到,對于自定義選項這不是簡單的替換,而是分別調用,當然我們也可以制定我們自己的合并策略:

const app = Vue.createApp({})app.config.optionMergeStrategies.customOption = (toVal, fromVal) => { // return mergedVal}

合并策略接收兩個參數,分別是指定項在父實例和子實例的值,當使用mixin的時候我們可以查看打印什么:

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打印。

注意事項

mixin很容易造成沖突,你得確保不會有沖突的屬性名,來避免沖突,這會造成額外的負擔 復用性有限,因為mixin不能接受參數,所以邏輯是寫定的,不靈活

所以官方推薦使用 Composition Api來組織邏輯

以上就是詳解Vue的mixin策略的詳細內容,更多關于Vue的mixin策略的資料請關注好吧啦網其它相關文章!

標簽: Vue
相關文章:
主站蜘蛛池模板: 婷婷色站 | 亚洲久久成人 | 国产日韩三级 | 日韩成人在线网站 | 免费精品视频 | 夜夜操天天干 | 一极黄色大片 | 午夜精品久久久久久久 | 97国产成人 | 国产精品区二区三区日本 | 成人一区二区三区视频 | 深夜福利在线视频 | 五月天精品在线 | 免费在线成人 | 天天做夜夜爽 | 成人在线精品 | 国产69久久精品成人看 | 免费视频99 | av免费在线网站 | 中文字幕在线视频网站 | 久久免费视频播放 | 免费黄网在线观看 | 免费成人深夜夜行网站 | 在线播放亚洲 | 中文字幕有码在线 | 一区二区三区视频在线播放 | 欧美日韩a v| 97久久精品人人澡人人爽 | 久久久久久久久99 | 久久久久久久久久久国产精品 | 国产一区二区三区四 | 国产一区二区三区高清 | 一级黄色免费看 | 亚洲一级精品 | 日本精品一区二区三区视频 | 高潮视频在线观看 | 日本免费黄色网址 | 狠狠夜夜 | 亚洲色图25p | 午夜丁香婷婷 | 热久久在线 |