亚洲精品久久久中文字幕-亚洲精品久久片久久-亚洲精品久久青草-亚洲精品久久婷婷爱久久婷婷-亚洲精品久久午夜香蕉

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

在Vue中使用HOC模式的實現

瀏覽:86日期:2022-12-03 17:23:32

前言

HOC是React常用的一種模式,但HOC只能是在React才能玩嗎?先來看看React官方文檔是怎么介紹HOC的:

高階組件(HOC)是React中用于復用組件邏輯的一種高級技巧。HOC自身不是ReactAPI的一部分,它是一種基于React的組合特性而形成的設計模式。

HOC它是一個模式,是一種思想,并不是只能在React中才能用。所以結合Vue的特性,一樣能在Vue中玩HOC。

HOC

HOC要解決的問題

并不是說哪種技術新穎,就得使用哪一種。得看這種技術能夠解決哪些痛點。

HOC主要解決的是可復用性的問題。在Vue中,這種問題一般是用Mixin解決的。Mixin是一種通過擴展收集功能的方式,它本質上是將一個對象的屬性拷貝到另一個對象上去。

最初React也是使用Mixin的,但是后面發現Mixin在React中并不是一種好的模式,它有以下的缺點:

mixin與組件之間容易導致命名沖突 mixin是侵入式的,改變了原組件,復雜性大大提高。

所以React就慢慢的脫離了mixin,從而推薦使用HOC。并不是mixin不優秀,只是mixin不適合React。

HOC是什么

HOC全稱:high-order component--也就是高階組件。具體而言,高階組件是參數為組件,返回值為新組件的函數。

而在React和Vue中組件就是函數,所以的高階組件其實就是高階函數,也就是返回一個函數的函數。

來看看HOC在React的用法:

function withComponent(WrappedComponent) { return class extends Component { componentDidMount () { console.log(’已經掛載完成’) } render() { return <WrappedComponent {...props} />; } }}

withComponent就是一個高階組件,它有以下特點:

HOC是一個純函數,且不應該修改原組件 HOC不關心傳遞的props是什么,并且WrappedComponent不關心數據來源 HOC接收到的props應該透傳給WrapperComponent

在Vue中使用HOC

怎么樣才能將Vue上使用HOC的模式呢?

我們一般書寫的Vue組件是這樣的:

<template> <div> <p>{{title}}</p> <button @click='changeTitle'></button> </div></template><script>export default { name: ’ChildComponent’, props: [’title’], methods: { changeTitle () { this.$emit(’changeTitle’); } }}</script>

而withComponet函數的功能是在每次掛載完成后都打印一句:已經掛載完成。

既然HOC是替代mixin的,所以我們先用mixin書寫一遍:

export default { mounted () { console.log(’已經掛載完成’) }}

然后導入到ChildComponent中

import withComponent from ’./withComponent’;export default { ... mixins: [’withComponet’],}

對于這個組件,我們在父組件中是這樣調用的

<child-component :title=’title’ @changeTitle=’changeTitle’></child-component><script>import ChildComponent from ’./childComponent.vue’;export default { ... components: {ChildComponent}}</script>

大家有沒有發現,當我們導入一個Vue組件時,其實是導入一個對象。

export default {}

至于說組件是函數,其實是經過處理之后的結果。所以Vue中的高階組件也可以是:接收一個純對象,返回一個純對象。

所以改為HOC模式,是這樣的:

export default function withComponent (WrapperComponent) { return { mounted () { console.log(’已經掛載完成’) }, props: WrappedComponent.props, render (h) { return h(WrapperComponent, {on: this.$listeners,attrs: this.$attrs,props: this.$props }) } }}

注意{on: this.$listeners,attr: this.$attrs, props: this.props}這一句就是透傳props的原理,等價于React中的<WrappedComponent {...props} />;

this.$props是指已經被聲明的props屬性,this.$attrs是指沒被聲明的props屬性。這一定要兩個一起透傳,缺少哪一個,props都不完整。

為了通用性,這里使用了render函數來構建,這是因為template只有在完整版的Vue中才能使用。

這樣似乎還不錯,但是還有一個重要的問題,在Vue組件中是可以使用插槽的。

比如:

<template> <div> <p>{{title}}</p> <button @click='changeTitle'></button> <slot></slot> </div></template>

在父組件中

<child-component :title=’title’ @changeTitle=’changeTitle’>Hello, HOC</child-component>

可以用this.$solts訪問到被插槽分發的內容。每個具名插槽都有其相應的property,例如v-slot:foo中的內容將會在this.$slots.foo中被找到。而default property包括了所有沒有被包含在具名插槽中的節點,或v-slot:default的內容。

所以在使用渲染函數書寫一個組件時,訪問this.$slots最有幫助的。

先將this.$slots轉化為數組,因為渲染函數的第三個參數是子節點,是一個數組

export default function withComponent (WrapperComponent) { return { mounted () { console.log(’已經掛載完成’) }, props: WrappedComponent.props, render (h) { const keys = Object.keys(this.$slots); const slotList = keys.reduce((arr, key) => arr.concat(this.$slots[key]), []); return h(WrapperComponent, {on: this.$listeners,attrs: this.$attrs,props: this.$props }, slotList) } }}

總算是有模有樣了,但這還沒結束,你會發現使不使用具名插槽都一樣,最后都是按默認插槽來處理的。

有點納悶,去看看Vue源碼中是怎么具名插槽的。在src/core/instance/render.js文件中找到了initRender函數,在初始化render函數時

const options = vm.$optionsconst parentVnode = vm.$vnode = options._parentVnode // the placeholder node in parent treeconst renderContext = parentVnode && parentVnode.contextvm.$slots = resolveSlots(options._renderChildren, renderContext)

這一段代碼是Vue解析并處理slot的。將vm.$options._parentVnode賦值為vm.$vnode,也就是$vnode就是父組件的vnode。如果父組件存在,定義renderContext = vm.$vnode.context。renderContext就是父組件要渲染的實例。 然后把renderContext和$options._renderChildren作為參數傳進resolveSlots()函數中。

接下里看看resolveSlots()函數,在src/core/instance/render-helper/resolve-slots.js文件中

export function resolveSlots ( children: ?Array<VNode>, context: ?Component): { [key: string]: Array<VNode> } { if (!children || !children.length) { return {} } const slots = {} for (let i = 0, l = children.length; i < l; i++) { const child = children[i] const data = child.data // remove slot attribute if the node is resolved as a Vue slot node if (data && data.attrs && data.attrs.slot) { delete data.attrs.slot } // named slots should only be respected if the vnode was rendered in the // same context. if ((child.context === context || child.fnContext === context) && data && data.slot != null ) { const name = data.slot const slot = (slots[name] || (slots[name] = [])) if (child.tag === ’template’) { slot.push.apply(slot, child.children || []) } else { slot.push(child) } } else { (slots.default || (slots.default = [])).push(child) } } // ignore slots that contains only whitespace for (const name in slots) { if (slots[name].every(isWhitespace)) { delete slots[name] } } return slots}

重點來看里面的一段if語句

// named slots should only be respected if the vnode was rendered in the// same context.if ((child.context === context || child.fnContext === context) && data && data.slot != null) { const name = data.slot const slot = (slots[name] || (slots[name] = [])) if (child.tag === ’template’) { slot.push.apply(slot, child.children || []) } else { slot.push(child) }} else { (slots.default || (slots.default = [])).push(child)}

只有當if ((child.context === context || child.fnContext === context) && data && data.slot != null ) 為真時,才處理為具名插槽,否則不管具名不具名,都當成默認插槽處理

else { (slots.default || (slots.default = [])).push(child)}

那為什么HOC上的if條件是不成立的呢?

這是因為由于HOC的介入,在原本的父組件與子組件之間插入了一個組件--也就是HOC,這導致了子組件中訪問的this.$vode已經不是原本的父組件的vnode了,而是HOC中的vnode,所以這時的this.$vnode.context引用的是高階組件,但是我們卻將slot透傳了,slot中的VNode的context引用的還是原來的父組件實例,所以就導致不成立。

從而都被處理為默認插槽。

解決方法也很簡單,只需手動的將slot中的vnode的context指向為HOC實例即可。注意當前實例 _self 屬性訪問當前實例本身,而不是直接使用 this,因為 this 是一個代理對象。

export default function withComponent (WrapperComponent) { return { mounted () { console.log(’已經掛載完成’) }, props: WrappedComponent.props, render (h) { const keys = Object.keys(this.$slots); const slotList = keys.reduce((arr, key) => arr.concat(this.$slots[key]), []).map(vnode => {vnode.context = this._selfreturn vnode }); return h(WrapperComponent, {on: this.$listeners,attrs: this.$attrs,props: this.$props }, slotList) } }}

而且scopeSlot與slot的處理方式是不同的,所以將scopeSlot一起透傳

export default function withComponent (WrapperComponent) { return { mounted () { console.log(’已經掛載完成’) }, props: WrappedComponent.props, render (h) { const keys = Object.keys(this.$slots); const slotList = keys.reduce((arr, key) => arr.concat(this.$slots[key]), []).map(vnode => {vnode.context = this._selfreturn vnode }); return h(WrapperComponent, {on: this.$listeners,attrs: this.$attrs,props: this.$props,scopedSlots: this.$scopedSlots }, slotList) } }}

這樣就行了。

結尾

更多文章請移步樓主github,如果喜歡請點一下star,對作者也是一種鼓勵。

到此這篇關于在Vue中使用HOC模式的文章就介紹到這了,更多相關Vue使用HOC模式內容請搜索好吧啦網以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持好吧啦網!

標簽: Vue
相關文章:
主站蜘蛛池模板: 国产网站在线播放 | 国产精品无卡无在线播放 | 在线观看黄色网 | 一级国产精品一级国产精品片 | 青草视频在线观看国产 | 色免费看 | 久久青青成人亚洲精品 | 精品国产欧美一区二区五十路 | www.色婷婷.com | 国产视频在线观看福利 | 国产精品久久精品福利网站 | 国产在线一区二区三区在线 | 婷婷亚洲久悠悠色在线播放 | 日本不卡中文字幕一区二区 | 6080亚洲| 亚洲欧洲网站 | 三级欧美在线 | 欧美综合偷拍在线另类卡通小说 | 91精品天美精东蜜桃传媒免费 | 一级毛片一级毛片免费毛片 | 精品日韩欧美国产一区二区 | 中文字幕久久网 | 一级毛片成人免费看a | 伊人伊成久久人综合网777 | 黄色大片在线免费看 | 国产成人综合亚洲欧美在 | 91免费视| 亚洲日韩精品欧美一区二区 | 看国产一级片 | 欧美成人爽毛片在线视频 | 五月伊人网 | 国产h版大片在线播放 | 成人看片又黄又爽 | 欧美v亚洲v国产v | 91看视频 | 在线观看香蕉视频 | 揄拍成人国产精品视频 | 国产夫妻啪啪 | 自拍影视 | 亚洲精品国产一区二区三区在 | 日本中文字幕不卡在线一区二区 |