Vue3?Composition?API優(yōu)雅封裝第三方組件實(shí)例
前言?
對(duì)于第三方組件,如何在保持第三方組件原有功能(屬性props、事件events、插槽slots、方法methods)的基礎(chǔ)上,優(yōu)雅地進(jìn)行功能的擴(kuò)展了?
以Element Plus的el-input為例:
很有可能你以前是這樣玩的,封裝一個(gè)MyInput組件,把要使用的屬性props、事件events和插槽slots、方法methods根據(jù)自己的需要再寫(xiě)一遍:
// 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>
可過(guò)一段時(shí)間后,需求變更,又要在MyInput組件上添加el-input組件的其它功能,可el-input組件總共有20個(gè)多屬性,5個(gè)事件,4個(gè)插槽,那該怎么辦呢,難道一個(gè)個(gè)傳進(jìn)去,這樣不僅繁瑣而且可讀性差。
在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。
//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>
當(dāng)然,這樣還不夠。光這樣寫(xiě),我們綁定的屬性(包括 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)撰寫(xiě)包裹一個(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處理,可以寫(xiě)成如下代碼:
// 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>
如果不滿足以上條件的話,咱還得老老實(shí)實(shí)在子組件中手動(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)雅封裝第三方組件實(shí)例的詳細(xì)內(nèi)容,更多關(guān)于Vue3 Composition API封裝第三方組件的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
vue3.0中setup中異步轉(zhuǎn)同步的實(shí)現(xiàn)
這篇文章主要介紹了vue3.0中setup中異步轉(zhuǎn)同步的實(shí)現(xiàn)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-06-06
手把手教你寫(xiě)一個(gè)vue全局注冊(cè)的Toast的實(shí)現(xiàn)
本文主要介紹了手把手教你寫(xiě)一個(gè)vue全局注冊(cè)的Toast的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-04-04
使用vue實(shí)現(xiàn)簡(jiǎn)單鍵盤(pán)的示例(支持移動(dòng)端和pc端)
這篇文章主要介紹了使用vue實(shí)現(xiàn)簡(jiǎn)單鍵盤(pán)的示例(支持移動(dòng)端和pc端),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-12-12
vue-quill-editor二次封裝,實(shí)現(xiàn)自定義文件上傳方式
這篇文章主要介紹了vue-quill-editor二次封裝,實(shí)現(xiàn)自定義文件上傳方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-03-03
Vue.js組件使用props傳遞數(shù)據(jù)的方法
這篇文章主要為大家詳細(xì)介紹了Vue.js組件使用props傳遞數(shù)據(jù)的方法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-10-10
vue中beforeRouteLeave實(shí)現(xiàn)頁(yè)面回退不刷新的示例代碼
這篇文章主要介紹了vue中beforeRouteLeave實(shí)現(xiàn)頁(yè)面回退不刷新的示例代碼,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-11-11
解決vue里a標(biāo)簽值解析變量,跳轉(zhuǎn)頁(yè)面,前面加默認(rèn)域名端口的問(wèn)題
這篇文章主要介紹了解決vue里a標(biāo)簽值解析變量,跳轉(zhuǎn)頁(yè)面,前面加默認(rèn)域名端口的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-07-07
解決vue3報(bào)錯(cuò):Unexpected?mutation?of?“xxx“?prop.(eslintvue/no
這篇文章主要給大家介紹了關(guān)于如何解決vue3報(bào)錯(cuò):Unexpected?mutation?of?“xxx“?prop.(eslintvue/no-mutating-props)的相關(guān)資料,文中通過(guò)代碼將解決辦法介紹的非常詳細(xì),需要的朋友可以參考下2023-12-12

