Vue3?Composition?API優(yōu)雅封裝第三方組件實例
前言?
對于第三方組件,如何在保持第三方組件原有功能(屬性props、事件events、插槽slots、方法methods)的基礎(chǔ)上,優(yōu)雅地進行功能的擴展了?
以Element Plus的el-input為例:
很有可能你以前是這樣玩的,封裝一個MyInput組件,把要使用的屬性props、事件events和插槽slots、方法methods根據(jù)自己的需要再寫一遍:
// 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>
可過一段時間后,需求變更,又要在MyInput組件上添加el-input組件的其它功能,可el-input組件總共有20個多屬性,5個事件,4個插槽,那該怎么辦呢,難道一個個傳進去,這樣不僅繁瑣而且可讀性差。
在Vue2中,我們可以這樣處理,點擊此處查看 封裝Vue第三方組件
此文詣在幫助大家做一個知識的遷移,探究如何使用Vue3 CompositionAPI優(yōu)雅地封裝第三方組件~
一、對于第三方組件的屬性props、事件events
在Vue2中
- $attrs: 包含了父作用域中不作為 prop 被識別 (且獲取) 的 attribute 綁定 (class 和 style 除外)。當一個組件沒有聲明任何prop 時,這里會包含所有父作用域的綁定 (class 和 style 除外),并且可以通過 v-bind="$attrs" 傳入內(nèi)部組件
- $listeners:包含了父作用域中的 (不含 .native 修飾器的) v-on 事件監(jiān)聽器。它可以通過 v-on="$listeners" 傳入內(nèi)部組件
而在Vue3中
- $attrs:包含了父作用域中不作為組件 props 或自定義事件的 attribute 綁定和事件(包括 class 和 style和自定義事件),同時可以通過 v-bind="$attrs" 傳入內(nèi)部組件。
- $listeners 對象在 Vue 3 中已被移除。事件監(jiān)聽器現(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>
當然,這樣還不夠。光這樣寫,我們綁定的屬性(包括 class 和 style)同時會在根元素(上面的例子是class="my-input"的Dom節(jié)點)上起作用。要阻止這個默認行為,我們需要設(shè)置inheritAttrs為false。
下面我們來看看Vue3文檔對inheritAttrs的解釋
默認情況下父作用域的不被認作 props 的 attribute 綁定 (attribute bindings) 將會“回退”且作為普通的 HTML attribute 應用在子組件的根元素上。當撰寫包裹一個目標元素或另一個組件的組件時,這可能不會總是符合預期行為。通過設(shè)置 inheritAttrs 到 false,這些默認行為將會被去掉。而通過實例 property $attrs 可以讓這些 attribute 生效,且可以通過 v-bind 顯性的綁定到非根元素上。
于是,我們對于第三方組件的屬性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>
二、對于第三方組件的插槽slots
Vue3中
- $slots:我們可以通過其拿到父組件傳入的插槽
- Vue3中移除了$scopedSlots,所有插槽都通過 $slots 作為函數(shù)暴露
- 在 <script setup>中輔助函數(shù)useSlots可以獲取到$slots。
基于以上幾點,如果我們對于第三方組件的封裝沒有增加額外的插槽,且第三方組件的插槽處于同一個dom節(jié)點之中,我們也有一種取巧的封裝方式??, 通過遍歷$slots拿到插槽的name,動態(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>
如果不滿足以上條件的話,咱還得老老實實在子組件中手動添加需要的第三方組件的插槽~
三、對于第三方組件的方法methods
對于第三方組件的方法,我們通過ref來實現(xiàn)。首先在MyInput組件中的el-input組件上添加一個ref="elInputRef"屬性,然后通過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>的組件里的屬性默認是關(guān)閉的,需通過defineExpose暴露出去才能被調(diào)用
})
</script>
父頁面: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) // 組件實例
onMounted(()=> {
myInput.value.elInputRef.focus() // 初始化時調(diào)用elInputRef實例的focus方法
})
</script>以上就是Vue3 Composition API優(yōu)雅封裝第三方組件實例的詳細內(nèi)容,更多關(guān)于Vue3 Composition API封裝第三方組件的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
vue3.0中setup中異步轉(zhuǎn)同步的實現(xiàn)
這篇文章主要介紹了vue3.0中setup中異步轉(zhuǎn)同步的實現(xiàn)方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-06-06
手把手教你寫一個vue全局注冊的Toast的實現(xiàn)
本文主要介紹了手把手教你寫一個vue全局注冊的Toast的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2022-04-04
使用vue實現(xiàn)簡單鍵盤的示例(支持移動端和pc端)
這篇文章主要介紹了使用vue實現(xiàn)簡單鍵盤的示例(支持移動端和pc端),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-12-12
vue-quill-editor二次封裝,實現(xiàn)自定義文件上傳方式
這篇文章主要介紹了vue-quill-editor二次封裝,實現(xiàn)自定義文件上傳方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-03-03
Vue.js組件使用props傳遞數(shù)據(jù)的方法
這篇文章主要為大家詳細介紹了Vue.js組件使用props傳遞數(shù)據(jù)的方法,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2019-10-10
vue中beforeRouteLeave實現(xiàn)頁面回退不刷新的示例代碼
這篇文章主要介紹了vue中beforeRouteLeave實現(xiàn)頁面回退不刷新的示例代碼,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2019-11-11
解決vue里a標簽值解析變量,跳轉(zhuǎn)頁面,前面加默認域名端口的問題
這篇文章主要介紹了解決vue里a標簽值解析變量,跳轉(zhuǎn)頁面,前面加默認域名端口的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-07-07
解決vue3報錯:Unexpected?mutation?of?“xxx“?prop.(eslintvue/no
這篇文章主要給大家介紹了關(guān)于如何解決vue3報錯:Unexpected?mutation?of?“xxx“?prop.(eslintvue/no-mutating-props)的相關(guān)資料,文中通過代碼將解決辦法介紹的非常詳細,需要的朋友可以參考下2023-12-12

