vue3繼承并擴展三方組件完成二次封裝的示例詳解
文章使用naiveui的Input組件進行舉例,elementPlus或者其他組件庫同理
繼承emits和props
<!-- TInput.vue --> <script setup lang="ts"> //導入三方組件提供的props import { NInput, inputProps } from 'naive-ui' //定義props const props = defineProps(inputProps) //擴展默認組件的type類型 const props = defineProps(inputProps) </script> <template> <NInput v-bind="props" ref="_ref"> </NInput> </template>
這樣就能完整的繼承組件的props和emits了 在另一個文件中檢查一下類型是否正確
很完美
擴展emits和props呢
我們利用assign方法來進行擴展 舉個例子,為placeholder添加一個前綴,這是原本輸入框的清除按鈕
<!-- TInput.vue --> <script setup lang="ts"> //導入三方組件提供的props import { NInput, inputProps } from 'naive-ui' import { assign } from 'lodash' import { computed, onMounted, ref } from 'vue' //擴展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>
可以看到我們對 placeholder設置的前綴生效了
繼承插槽
我們可以通過useSlots
或者$slots
來獲取到接受到的插槽,然后只要簡單的循環(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>
擴展插槽
舉個例子,我想修改NInput默認的清除按鈕,但又保留使用插槽修改清除按鈕的功能 這是原本的清除按鈕
現(xiàn)在將這個清除按鈕的默認值修改為c 為插槽設置默認值
<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>
當我 沒有使用clear-icon的插槽的時候,可以看到
當我使用clear-icon的插槽的時候
<TInput v-model:value="val" :suffix="'suffix'" clearable> <template #clear-icon> <div>q</div> </template> </TInput>
插槽沒有類型提示
需要檢查你使用的三方組件是否導出了Slots的類型,然后在defineSlots中使用這個類型,使用類型工具擴展.但是由于組件泛型并不好用.定義slots的類型意義不大. 由于naiveui并沒有導出Slots的類型,因此這里不做演示
繼承組件實例提供的方法
繼承組件實例的方法需要在Mounted的生命周期中,獲取組件實例并且將其導出
<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組件的時候引入組件實例的類型,實現(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添加鍵值對,因為對組件實例的操作會讓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. 因此簡單的把組件實例通過expose的函數(shù)導出即可
到此這篇關(guān)于vue3繼承并擴展三方組件完成二次封裝的示例詳解的文章就介紹到這了,更多相關(guān)vue3繼承并擴展三方組件內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
axios請求頭設置常見Content-Type和對應參數(shù)的處理方式
這篇文章主要介紹了axios請求頭設置常見Content-Type和對應參數(shù)的處理方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-03-03Vue路由this.route.push跳轉(zhuǎn)頁面不刷新的解決方案
這篇文章主要介紹了Vue路由this.route.push跳轉(zhuǎn)頁面不刷新的解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-07-07Vue?ElementUI?table實現(xiàn)表格斜線分隔線
這篇文章主要為大家詳細介紹了Vue?ElementUI?table實現(xiàn)表格斜線分隔線,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-03-03VueJs里利用CryptoJs實現(xiàn)加密及解密的方法示例
這篇文章主要介紹了VueJs里利用CryptoJs實現(xiàn)加密及解密的方法示例,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2019-04-04