vue3繼承并擴(kuò)展三方組件完成二次封裝的示例詳解
文章使用naiveui的Input組件進(jìn)行舉例,elementPlus或者其他組件庫(kù)同理
繼承emits和props
<!-- TInput.vue -->
<script setup lang="ts">
//導(dǎo)入三方組件提供的props
import { NInput, inputProps } from 'naive-ui'
//定義props
const props = defineProps(inputProps)
//擴(kuò)展默認(rèn)組件的type類型
const props = defineProps(inputProps)
</script>
<template>
<NInput v-bind="props" ref="_ref">
</NInput>
</template>
這樣就能完整的繼承組件的props和emits了 在另一個(gè)文件中檢查一下類型是否正確

很完美
擴(kuò)展emits和props呢
我們利用assign方法來進(jìn)行擴(kuò)展 舉個(gè)例子,為placeholder添加一個(gè)前綴,這是原本輸入框的清除按鈕
<!-- TInput.vue -->
<script setup lang="ts">
//導(dǎo)入三方組件提供的props
import { NInput, inputProps } from 'naive-ui'
import { assign } from 'lodash'
import { computed, onMounted, ref } from 'vue'
//擴(kuò)展placeholder的前綴
const props = defineProps(
assign(inputProps, {
placeholder: {
type: String,
default: 'default-input'
},
suffix: {
type: String,
default: ''
}
})
)
const _placeHolder = computed(() => props?.suffix + props?.placeholder)
</script>
<template>
<NInput v-bind="props" :placeholder="_placeHolder" ref="_ref"> </NInput>
</template>
//使用TInput組件 <template> <TInput v-model:value="val" :suffix="'suffix'"></TInput> </template>

可以看到我們對(duì) placeholder設(shè)置的前綴生效了
繼承插槽
我們可以通過useSlots或者$slots來獲取到接受到的插槽,然后只要簡(jiǎn)單的循環(huán)就能繼承插槽了
<template>
<NInput v-bind="props" ref="_ref" :placeholder="_placeHolder">
<template v-for="(_, name) in $slots" #[name]="slotData">
<slot :name v-bind="slotData || {}"></slot>
</template>
</NInput>
</template>
擴(kuò)展插槽
舉個(gè)例子,我想修改NInput默認(rèn)的清除按鈕,但又保留使用插槽修改清除按鈕的功能 這是原本的清除按鈕

現(xiàn)在將這個(gè)清除按鈕的默認(rèn)值修改為c 為插槽設(shè)置默認(rèn)值
<template>
<NInput v-bind="props" ref="_ref" :placeholder="_placeHolder">
<template #clear-icon><div>c</div></template>
<template v-for="(_, name) in $slots" #[name]="slotData" :key="name">
<slot :name v-bind="slotData || {}"></slot>
</template>
</NInput>
</template>
當(dāng)我 沒有使用clear-icon的插槽的時(shí)候,可以看到

當(dāng)我使用clear-icon的插槽的時(shí)候
<TInput v-model:value="val" :suffix="'suffix'" clearable>
<template #clear-icon>
<div>q</div>
</template>
</TInput>

插槽沒有類型提示
需要檢查你使用的三方組件是否導(dǎo)出了Slots的類型,然后在defineSlots中使用這個(gè)類型,使用類型工具擴(kuò)展.但是由于組件泛型并不好用.定義slots的類型意義不大. 由于naiveui并沒有導(dǎo)出Slots的類型,因此這里不做演示
繼承組件實(shí)例提供的方法
繼承組件實(shí)例的方法需要在Mounted的生命周期中,獲取組件實(shí)例并且將其導(dǎo)出
<template>
<NInput ref="_ref" >
</NInput>
</template>
<script setup lang="ts">
const _ref = ref()
const _expose: Record<string, any> = {
getInst: () => {
return _ref.value
}
}
defineExpose(_expose)
</script>
在使用TInput組件的時(shí)候引入組件實(shí)例的類型,實(shí)現(xiàn)良好的類型提示
<script setup lang="ts">
import TInput from './components/TInput.vue'
import { onMounted, ref } from 'vue'
import { type InputInst } from 'naive-ui'
const val = ref()
const inputRef = ref< { getInst: () => InputInst }>()
onMounted(() => {
console.log(inputRef.value?.getInst?.()?.focus())
})
</script>
<template>
<TInput ref="inputRef" v-model:value="val" :suffix="'suffix'" clearable> </TInput>
</template>
注意,不要使用循環(huán)的方式為expose添加鍵值對(duì),因?yàn)閷?duì)組件實(shí)例的操作會(huì)讓vue提交警告, `void app logic that relies on enumerating keys on a component instance. The keys will be empty in production mode to avoid performance overhead. 因此簡(jiǎn)單的把組件實(shí)例通過expose的函數(shù)導(dǎo)出即可
到此這篇關(guān)于vue3繼承并擴(kuò)展三方組件完成二次封裝的示例詳解的文章就介紹到這了,更多相關(guān)vue3繼承并擴(kuò)展三方組件內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
axios請(qǐng)求頭設(shè)置常見Content-Type和對(duì)應(yīng)參數(shù)的處理方式
這篇文章主要介紹了axios請(qǐng)求頭設(shè)置常見Content-Type和對(duì)應(yīng)參數(shù)的處理方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-03-03
Vue手動(dòng)控制點(diǎn)擊事件Click觸發(fā)方式
這篇文章主要介紹了Vue手動(dòng)控制點(diǎn)擊事件Click觸發(fā)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-01-01
基于vue實(shí)現(xiàn)一個(gè)禪道主頁拖拽效果
最近在做一個(gè)基于vue的后臺(tái)管理項(xiàng)目。接下來通過本文給大家分析一款基于vue做一個(gè)禪道主頁拖拽效果,需要的朋友可以參考下2019-05-05
Vue路由this.route.push跳轉(zhuǎn)頁面不刷新的解決方案
這篇文章主要介紹了Vue路由this.route.push跳轉(zhuǎn)頁面不刷新的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-07-07
Vue?ElementUI?table實(shí)現(xiàn)表格斜線分隔線
這篇文章主要為大家詳細(xì)介紹了Vue?ElementUI?table實(shí)現(xiàn)表格斜線分隔線,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-03-03
vue?LogicFlow更多配置選項(xiàng)示例詳解
這篇文章主要為大家介紹了vue?LogicFlow更多配置選項(xiàng)詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-01-01
VueJs里利用CryptoJs實(shí)現(xiàn)加密及解密的方法示例
這篇文章主要介紹了VueJs里利用CryptoJs實(shí)現(xiàn)加密及解密的方法示例,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2019-04-04
vue如何實(shí)現(xiàn)點(diǎn)擊選中取消切換
這篇文章主要介紹了vue實(shí)現(xiàn)點(diǎn)擊選中取消切換,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-05-05

