我們時(shí)常會(huì)想在保持第三方組件原有功能(屬性props、事件events、插槽slots、方法methods)的基礎(chǔ)上,這些功能如何“優(yōu)化”的實(shí)現(xiàn)?
以Element Plus的el-input為例:
在封裝一個(gè)MyInput組件,把要使用的屬性props、事件events和插槽slots、方法methods先要依照自己的需求來(lái)編寫:
// MyInput.vue <template> <div class="my-input"> <el-input v-model="inputVal" :clearable="clearable" @clear="clear"> <template #prefix> <slot name="prefix"></slot> </template> <template #suffix> <slot name="suffix"></slot> </template> </el-input> </div> </template> <script setup> import { computed } from 'vue' const props = defineProps({ modelValue: { type: String, default: '' }, clearable: { type: Boolean, default: false } }) const emits = defineEmits(['update:modelValue', 'clear']) const inputVal = computed({ get: () => props.modelValue, set: (val) => { emits('update:modelValue', val) } }) const clear = () => { emits('clear') } </script>
但當(dāng)需求發(fā)生變動(dòng),此時(shí)又要在MyInput組件上添加el-input組件的其它功能,可el-input組件總共有20個(gè)多屬性,5個(gè)事件,4個(gè)插槽,這樣是不是很繁瑣,對(duì)我們這些想要“懶”的人,事不對(duì)不可以的,那該怎么辦呢。
在Vue2中,我們可以這樣處理,點(diǎn)擊此處查看封裝Vue第三方組件
此文詣在幫助大家做一個(gè)知識(shí)的遷移,探究如何使用Vue3 CompositionAPI優(yōu)雅地封裝第三方組件~
一、對(duì)于第三方組件的屬性props、事件events
在Vue2中
$attrs: 包含了父作用域中不作為 prop 被識(shí)別 (且獲取) 的 attribute 綁定 (class 和 style 除外)。當(dāng)一個(gè)組件沒(méi)有聲明任何prop 時(shí),這里會(huì)包含所有父作用域的綁定 (class 和 style 除外),并且可以通過(guò) v-bind="$attrs" 傳入內(nèi)部組件
$listeners:包含了父作用域中的 (不含.native修飾器的)v-on事件監(jiān)聽(tīng)器。它可以通過(guò)v-on="$listeners"傳入內(nèi)部組件
而在Vue3中
$attrs:包含了父作用域中不作為組件props或自定義事件的 attribute 綁定和事件(包括class和style和自定義事件),同時(shí)可以通過(guò) v-bind="$attrs" 傳入內(nèi)部組件。
$listeners對(duì)象在 Vue 3 中已被移除。事件監(jiān)聽(tīng)器現(xiàn)在是$attrs的一部分。
在<script setup>中輔助函數(shù)useAttrs可以獲取到$attrs?!?/p>
//MyInput.vue <template> <div class="my-input"> <el-input v-bind="attrs"></el-input> </div> </template> <script setup> import { useAttrs } from 'vue' const attrs = useAttrs() </script>
就算加上上面這些也是不夠的?,F(xiàn)在我們綁定的屬性(包括class和style)同時(shí)會(huì)在根元素(上面的例子是class="my-input"的Dom節(jié)點(diǎn))上起作用。要阻止這個(gè)默認(rèn)行為,我們需要設(shè)置inheritAttrs為false。
下面我們來(lái)看看Vue3文檔對(duì)inheritAttrs的解釋
默認(rèn)情況下父作用域的不被認(rèn)作 props 的 attribute 綁定 (attribute bindings) 將會(huì)“回退”且作為普通的 HTML attribute 應(yīng)用在子組件的根元素上。當(dāng)撰寫包裹一個(gè)目標(biāo)元素或另一個(gè)組件的組件時(shí),這可能不會(huì)總是符合預(yù)期行為。通過(guò)設(shè)置inheritAttrs到false,這些默認(rèn)行為將會(huì)被去掉。而通過(guò)實(shí)例 property$attrs可以讓這些 attribute 生效,且可以通過(guò)v-bind顯性的綁定到非根元素上。
于是,我們對(duì)于第三方組件的屬性props、事件events處理,可以寫成如下代碼:
// MyInput.vue <template> <div class="my-input"> <el-input v-bind="attrs"></el-input> </div> </template> <script> export default { name: 'MyInput', inheritAttrs: false } </script> <script setup> import { useAttrs } from 'vue' const attrs = useAttrs() </script>
二、對(duì)于第三方組件的插槽slots
Vue3中
$slots:我們可以通過(guò)其拿到父組件傳入的插槽
Vue3中移除了$scopedSlots,所有插槽都通過(guò)$slots作為函數(shù)暴露
在<script setup>中輔助函數(shù)useSlots可以獲取到$slots。
從上面幾點(diǎn)看到,我們對(duì)于第三方組件的封裝沒(méi)有增加額外的插槽,且第三方組件的插槽處于同一個(gè)dom節(jié)點(diǎn)之中,我們也有一種取巧的封裝方式????, 通過(guò)遍歷$slots拿到插槽的name,動(dòng)態(tài)添加子組件的插槽:
//MyInput.vue <template> <div class="my-input"> <el-input v-bind="attrs"> <template v-for="k in Object.keys(slots)" #[k] :key="k"> <slot :name="k"></slot> </template> </el-input> </div> </template> <script> export default { name: 'MyInput', inheritAttrs: false } </script> <script setup> import { useAttrs, useSlots } from 'vue' const attrs = useAttrs() const slots = useSlots() </script>
上再不行,沒(méi)辦法就只要好,在子組件中手動(dòng)添加需要的第三方組件的插槽~
三、對(duì)于第三方組件的方法methods
對(duì)于第三方組件的方法,我們通過(guò)ref來(lái)實(shí)現(xiàn)。首先在MyInput組件中的el-input組件上添加一個(gè)ref="elInputRef"屬性,然后通過(guò)defineExpose把elInputRef暴露出去給父組件調(diào)用。
子組件:MyInput.vue
// MyInput.vue <template> <div class="my-input"> <el-input v-bind="attrs" ref="elInputRef"> <template v-for="k in Object.keys(slots)" #[k] :key="k"> <slot :name="k"></slot> </template> </el-input> </div> </template> <script> export default { name: 'MyInput', inheritAttrs: false } </script> <script setup> import { useAttrs, useSlots } from 'vue' const attrs = useAttrs() const slots = useSlots() const elInputRef = ref(null) defineExpose({ elInputRef // <script setup>的組件里的屬性默認(rèn)是關(guān)閉的,需通過(guò)defineExpose暴露出去才能被調(diào)用 }) </script>
父頁(yè)面:Index.vue的調(diào)用代碼如下:
// Index.vue <template> <my-input v-model='input' ref="myInput"> <template #prefix>姓名</template> </my-input> </template> <script setup> import MyInput from './components/MyInput.vue' import { ref, onMounted } from 'vue' const input = ref('') const myInput = ref(null) // 組件實(shí)例 onMounted(()=> { myInput.value.elInputRef.focus() // 初始化時(shí)調(diào)用elInputRef實(shí)例的focus方法 }) </script>
以上就是Vue3 Composition API優(yōu)雅封裝第三方組件的詳細(xì)內(nèi)容,請(qǐng)大家好好學(xué)習(xí)深入了解。
文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請(qǐng)注明本文地址:http://systransis.cn/yun/128233.html
摘要:五六月份推薦集合查看最新的請(qǐng)點(diǎn)擊集前端最近很火的框架資源定時(shí)更新,歡迎一下。蘇幕遮燎沈香宋周邦彥燎沈香,消溽暑。鳥(niǎo)雀呼晴,侵曉窺檐語(yǔ)。葉上初陽(yáng)乾宿雨,水面清圓,一一風(fēng)荷舉。家住吳門,久作長(zhǎng)安旅。五月漁郎相憶否。小楫輕舟,夢(mèng)入芙蓉浦。 五、六月份推薦集合 查看github最新的Vue weekly;請(qǐng)::點(diǎn)擊::集web前端最近很火的vue2框架資源;定時(shí)更新,歡迎 Star 一下。 蘇...
摘要:五六月份推薦集合查看最新的請(qǐng)點(diǎn)擊集前端最近很火的框架資源定時(shí)更新,歡迎一下。蘇幕遮燎沈香宋周邦彥燎沈香,消溽暑。鳥(niǎo)雀呼晴,侵曉窺檐語(yǔ)。葉上初陽(yáng)乾宿雨,水面清圓,一一風(fēng)荷舉。家住吳門,久作長(zhǎng)安旅。五月漁郎相憶否。小楫輕舟,夢(mèng)入芙蓉浦。 五、六月份推薦集合 查看github最新的Vue weekly;請(qǐng)::點(diǎn)擊::集web前端最近很火的vue2框架資源;定時(shí)更新,歡迎 Star 一下。 蘇...
摘要:哪吒別人的看法都是狗屁,你是誰(shuí)只有你自己說(shuō)了才算,這是爹教我的道理。哪吒去他個(gè)鳥(niǎo)命我命由我,不由天是魔是仙,我自己決定哪吒白白搭上一條人命,你傻不傻敖丙不傻誰(shuí)和你做朋友太乙真人人是否能夠改變命運(yùn),我不曉得。我只曉得,不認(rèn)命是哪吒的命。 showImg(https://segmentfault.com/img/bVbwiGL?w=900&h=378); 出處 查看github最新的Vue...
在近期的工作中有些知識(shí)總結(jié)分享就是使用 uniapp 的 Vue3 版進(jìn)行開(kāi)發(fā)。這樣可以在開(kāi)發(fā)中遇到業(yè)務(wù)場(chǎng)景相同的,就分裝了一個(gè)hook 來(lái)減少代碼,易于維護(hù)?! ook的場(chǎng)景 上圖中已經(jīng)很詳細(xì)為我們展示3處使用到了獲取列表的功能。分別是: 我的收藏、已投遞崗位、未投遞崗位?,F(xiàn)在我們就來(lái)詳細(xì)說(shuō)說(shuō)。 假如: 我的收藏、已投遞崗位、未投遞崗位 都各自獲取列表,就會(huì)出現(xiàn)重復(fù)性的定義以下代碼 ...
閱讀 596·2023-03-27 18:33
閱讀 790·2023-03-26 17:27
閱讀 684·2023-03-26 17:14
閱讀 645·2023-03-17 21:13
閱讀 573·2023-03-17 08:28
閱讀 1895·2023-02-27 22:32
閱讀 1376·2023-02-27 22:27
閱讀 2280·2023-01-20 08:28