Vue組件二次封裝的一些實用技巧總結(jié)
前言
在開發(fā)Vue項目我們一般使用第三方UI組件庫進行開發(fā),如element-plus, @arco-design/web-Vue, naive-ui等, 但是這些組件提供的接口并不一定滿足我們的需求,這時我們可以通過對組件庫組件的二次封裝,來滿足我們特殊的需求。
對于封裝組件有一個大原則就是我們應(yīng)該盡量保持原有組件的接口,除了我們需要封裝的功能外,我們不應(yīng)該改變原有組件的接口,即保持原有組件提供的接口如props,events,slots等不變。
為了實現(xiàn)這一原則我們就需要將新組件的接口與舊組件的接口一一對應(yīng), 當(dāng)然我們可以通過在新組件中一一聲明對應(yīng)的接口來實現(xiàn)(或者只實現(xiàn)我們目前需要用到的接口)但這種辦法雖然簡單但看起來卻極其很繁瑣, 有沒有一種方法可以實現(xiàn)props,events,slots的自動透傳呢?
透傳 Attribute
我們可以使用一個沒有參數(shù)的 v-bind來實現(xiàn)props,events的透傳, 它會將一個對象的所有屬性都作為 attribute 應(yīng)用到目標(biāo)元素或組件上, 這在官方文檔中有著詳細介紹。
<BaseButton v-bind="$attrs"/>
其中$attrs包含組件可以透傳屬性的對象, 透傳屬性包括props,events, class,style,id等。(不包含接收組件顯式聲明的 props、emits以及slots )
如下,是一個封裝el-input的默認可清空的的組件,由于我們已經(jīng)在defineProps聲明過clearable, 所以此時我們需要顯性傳遞clearable屬性
<template> <div class="my-input"> {{ label }} <el-input v-bind="$attrs" :clearable="clearable"></el-input> </div> </template> <script setup> defineProps({ label: String, clearable: { type: Boolean, default: true, }, }); </script>
如果我們不希望透傳某些屬性比如class, 我們可以通過useAttrs來實現(xiàn)
<template> <div class="my-input"> {{ label }} <el-input v-bind="filteredAttrs" :clearable="clearable"></el-input> </div> </template> <script setup> import { computed, useAttrs } from 'Vue'; defineProps({ label: String, clearable: { type: Boolean, default: true, }, }); const attrs = useAttrs(); const filteredAttrs = computed(() => { return { ...attrs, class: undefined }; }); </script>
上述封裝的組件還有個缺點, 就是我們將無法使用el-input本身提供的slot,下面我們就來實現(xiàn)一個可以透傳 slot的組件
透傳 slot
slot可以通過下面這種方式透傳的
<!-- 在組件中創(chuàng)建新的對應(yīng)名稱的插槽 --> <template #slotName> <!-- 在插槽內(nèi)部使用對應(yīng)名稱的插槽 --> <slot name="slotName" /> </template>
普通slot
如果透傳的slot比較少,我們可以通過在封裝組件內(nèi)部定義并使用插槽<template v-slot:slotName><slot name="slotName" /></template>來透傳插槽
<template #slotName> <slot name="slotName" /> </template>
動態(tài)插槽名
如果需要透傳的slot不固定或者較多,我們可以通過動態(tài)插槽名稱透傳
<template #[slotName] v-for="(slot, slotName) in $slots" > <slot :name="slotName" /> </template>
如下是一個透傳的slot的el-input組件
<template> <div class="my-input"> {{ label }} <el-input v-bind="$attrs" :clearable="clearable"> <template #[slotName] v-for="(slot, slotName) in $slots"> <slot :name="slotName" /> </template> </el-input> </div> </template> <script setup> defineProps({ label: String, clearable: { type: Boolean, default: true, }, }); </script>
作用域插槽
如果需要封裝組件使用了作用域插槽,我們可以通過<template v-slot:slotName="slotProps"><slot name="slotName" v-bind="slotProps"/></template>來透傳作用域插槽插槽。
<template #[slotName]="slotProps" v-for="(slot, slotName) in $slots" > <slot :name="slotName" v-bind="slotProps"/> </template>
封裝組件存在的問題
組件實例屬性和方法的調(diào)用
封裝后的組件我們無法按照之前的情況調(diào)用組件實例中的屬性和方法,比如BaseButton有focus方法,在封裝之前我們可以通過下面這種方式調(diào)用
// 調(diào)用BaseButton的組件父組件 // <BaseButton ref="button"> const button = ref(); button.value.focus()
對于封裝后的組件,由于此時button指向我們的MyButton,并不指向BaseButton的實例,所以我們需要在包裝的組件中聲明并暴露BaseButton事例
//我們封裝的組件 // MyButton.Vue // <BaseButton ref="button"> const button = ref();
注意如果我們使用 <script setup>,是沒辦法訪問實例中的屬性的,詳情參考vuejs.org/api/sfc-scr…
此時可以通過defineExpose顯式公開ref屬性
// 我們封裝的組件 // MyButton.Vue // <BaseButton ref="button"> const button = ref(); defineExpose({ button });
然后在父組件中我就可以通過實例鏈?zhǔn)秸{(diào)用封裝的組件了
// <MyButton ref="button"> const button = ref(); button.value.button.focus()
總結(jié)
到此這篇關(guān)于Vue組件二次封裝的一些實用技巧的文章就介紹到這了,更多相關(guān)Vue組件二次封裝內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Vue?axios和vue-axios的關(guān)系及使用區(qū)別
axios是基于promise的HTTP庫,可以使用在瀏覽器和node.js中,它不是vue的第三方插件,vue-axios是axios集成到Vue.js的小包裝器,可以像插件一樣安裝使用:Vue.use(VueAxios, axios),本文給大家介紹Vue?axios和vue-axios關(guān)系,感興趣的朋友一起看看吧2022-08-08vue3.0報錯Cannot?find?module‘worker_threads‘的解決辦法
這篇文章介紹了vue3.0報錯Cannot?find?module‘worker_threads‘的解決辦法。對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-11-11vue 監(jiān)聽是否切屏和開啟小窗的實現(xiàn)過程
這篇文章主要介紹了vue 監(jiān)聽是否切屏和開啟小窗的過程,本文通過實例代碼給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-04-04vue使用require.context實現(xiàn)動態(tài)注冊路由
這篇文章主要介紹了vue使用require.context實現(xiàn)動態(tài)注冊路由的方法,幫助大家更好的理解和使用vue框架,感興趣的朋友可以了解下2020-12-12