欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Vue3實現(xiàn)組件二次封裝的小技巧分享

 更新時間:2024年09月12日 09:01:21   作者:唐詩  
組件的二次封裝:保留組件已有的功能,需要重寫組件方法,當(dāng)組件已有大量功能時候,則需要重寫很多重復(fù)代碼,且組件功能進行修改的時候,封裝的組件也需要對應(yīng)修改,從而造成許多開發(fā)和維護成本,本文給大家分享了Vue3實現(xiàn)組件二次封裝的小技巧,需要的朋友可以參考下

雙向數(shù)據(jù)綁定

我們以 input 組件作為例子

雙向數(shù)據(jù)綁定的原理及實現(xiàn)想必大家已經(jīng)爛熟于心了直接看官網(wǎng)吧!

子組件接受一個 modelValue 的 prop, 通過 emit 觸發(fā) update:modelValue 事件完成數(shù)據(jù)的更新

父組件直接 v-model="xxxx"

嫌麻煩官方還提供了 defineModel() 用于簡化上邊的步驟

向子組件傳遞插槽

我們以 input 組件作為例子,創(chuàng)建一個 WrapInput.vue 組件

未學(xué)習(xí)之前

WrapInput.vue 常規(guī)的做法,遍歷 $slots 來實現(xiàn)

<script setup lang="ts">
const model = defineModel()

</script>

<template>
  <el-input v-model="model" placeholder="Please input" >
    <template v-for="(_, slot) in $slots" :key="solt" v-slot:[slot]="slotProps">
      <slot :name="slot" v-bind="slotProps"></slot>
    </template>
  </el-input>
</template>

<style lang='scss' scoped></style>

在 app.vue 中引入并傳遞 prepend、append 插槽

<script setup lang="ts">
import { ref } from "vue";
import WrapInput from "./components/WrapInput.vue";

const inputText = ref('')
</script>

<template>
  <WrapInput v-model="inputText">
    <template #prepend>Http://</template>
    <template #append>.com</template>
  </WrapInput>

  <div>
    {{inputText}}
  </div>
</template>

<style scoped>
</style>

正確渲染了插槽

學(xué)習(xí)之后

讓我們來修改下 WrapInput.vue

<script setup lang="ts">
import { h } from "vue";
import { ElInput } from "element-plus";
const model = defineModel()
</script>

<template>
<component :is="h(ElInput, $attrs, $slots)" v-model="model"></component>
</template>

<style lang='scss' scoped></style>

app.vue 的代碼不做任何修改

插槽正常傳遞、數(shù)據(jù)更新正常,看到這種寫法的時候有點震驚的

component 組件為什么可以傳入 h 函數(shù)

看下 h 函數(shù)的文檔, h(ElInput, $attrs, $slots) 是創(chuàng)建了一個虛擬 dom 節(jié)點

而 component 組件的 is 屬性則可以接收

  • 被注冊的組件名
  • 導(dǎo)入的組件對象
  • 一個返回上述值之一的函數(shù)

當(dāng) component 組件的 is 屬性接收到一個函數(shù)時,Vue 會調(diào)用這個函數(shù)并使用其返回值作為要渲染的組件。

在這種情況下,h(ElInput, $attrs, $slots) 會立即執(zhí)行并返回一個 VNode,這個 VNode 描述了如何渲染 ElInput 組件。

獲取子組件的 ref

未學(xué)習(xí)之前

之前的自己的寫法有點蠢的具體的做法是在子組件創(chuàng)建一個 getRef 的函數(shù)把 ref 暴露出去,父組件調(diào)用 getRef 方法后在執(zhí)行子組件方法的調(diào)用,大概是下邊這樣

WrapInput1.vue

<script setup lang="ts">
import { h, ref} from "vue";
import { ElInput } from "element-plus";
const model = defineModel()

const inputRef = ref()

function getRef () {
  return inputRef.value
}

defineExpose({
  getRef
})
</script>

<template>
  <component ref="inputRef" :is="h(ElInput, $attrs, $slots)" v-model="model"></component>
</template>

<style lang='scss' scoped></style>

學(xué)習(xí)之后

WrapInput.vue

<script setup lang="ts">
import { h, ref } from "vue";
import { ElInput } from "element-plus";
const model = defineModel()

const inputRef = ref()

defineExpose(new Proxy({}, {
  get(_target, prop)  {
    return inputRef.value?.[prop]
  },
  has (_target, prop) {
    return prop in inputRef.value
  }
}))

</script>

<template>
  <component :is="h(ElInput, $attrs, $slots)" v-model="model" ref="inputRef"></component>
</template>

<style lang='scss' scoped></style>

使用 Proxy 代理暴露出去的方法,是有點震驚的,還能這么寫

App.vue

<script setup lang="ts">
import { ref } from "vue";
import WrapInput from "./components/WrapInput.vue";

const inputText = ref('')

const prependSlotText =  ref('Http://')
const appendSlotText =  ref('.com')

function updateSlotInfo (){
  prependSlotText.value = 'https://'
  appendSlotText.value = `${new Date().getTime()}`
}

const wrapInputRef = ref()
function setWrapInputFocus () {
  wrapInputRef.value?.focus()
}
</script>

<template>
  <WrapInput v-model="inputText" ref="wrapInputRef">
    <template #prepend>{{ prependSlotText }}</template>
    <template #append>{{ appendSlotText }}</template>
  </WrapInput>

  <div style="margin: 20px 0;">
    {{inputText}}
  </div>

  <el-button type="primary" @click="updateSlotInfo">更新插槽內(nèi)容</el-button>
  <el-button type="primary" @click="setWrapInputFocus">set input focus</el-button>
  
</template>

<style scoped>
</style>

調(diào)用組件的 focus 方法讓 WrapInput.vue 組件獲取焦點

總結(jié)

本文實踐了在 vue3 中在二次封裝組件時如何實現(xiàn) v-model、插槽傳遞、子組件 ref 獲取

插槽傳遞通過向 component 組件的 is 屬性傳遞 h 函數(shù)創(chuàng)建虛擬 dom 來實現(xiàn)

獲取子組件的 ref 則是使用 new Proxy 的方式來實現(xiàn)

以上就是Vue3實現(xiàn)組件二次封裝的小技巧分享的詳細(xì)內(nèi)容,更多關(guān)于Vue3組件二次封裝的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • 一文詳解Vue.js生產(chǎn)環(huán)境文件及優(yōu)化策略

    一文詳解Vue.js生產(chǎn)環(huán)境文件及優(yōu)化策略

    隨著 Vue.js 在前端開發(fā)中的普及,如何高效地將 Vue 項目部署到生產(chǎn)環(huán)境成為了開發(fā)者關(guān)注的重點,本文將詳細(xì)解析 Vue.js 生產(chǎn)環(huán)境文件的使用方法、優(yōu)缺點以及優(yōu)化策略,需要的朋友可以參考下
    2024-12-12
  • vue?+?electron應(yīng)用文件讀寫操作

    vue?+?electron應(yīng)用文件讀寫操作

    這篇文章主要介紹了vue?+?electron應(yīng)用文件讀寫操作,如果要制作的應(yīng)用并不復(fù)雜,完全可以將數(shù)據(jù)存儲在本地文件當(dāng)中,然后應(yīng)用就可以通過這些文件進行數(shù)據(jù)的讀寫,需要的朋友參考下吧
    2022-06-06
  • vue?點擊刪除常用方式小結(jié)

    vue?點擊刪除常用方式小結(jié)

    這篇文章主要介紹了vue?點擊刪除常用方式小結(jié),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-04-04
  • 解決VantUI popup 彈窗不彈出或無蒙層的問題

    解決VantUI popup 彈窗不彈出或無蒙層的問題

    這篇文章主要介紹了解決VantUI popup 彈窗不彈出或無蒙層的問題。具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-11-11
  • vue使用canvas手寫輸入識別中文

    vue使用canvas手寫輸入識別中文

    這篇文章主要介紹了vue使用canvas手寫輸入識別中文,工作時遇到一些項目如:系統(tǒng)上的輸入法使用不方便,客戶要求做一個嵌入web網(wǎng)頁的手寫輸入法。下面我們來看看文章得具體描述吧
    2021-11-11
  • vue前端重構(gòu)computed及watch組件通信等實用技巧整理

    vue前端重構(gòu)computed及watch組件通信等實用技巧整理

    這篇文章主要為大家介紹了vue前端重構(gòu)computed及watch組件通信等實用技巧整理,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-05-05
  • vue2+element-ui新增編輯表格+刪除行功能

    vue2+element-ui新增編輯表格+刪除行功能

    這篇文章主要介紹了vue2+element-ui新增編輯表格+刪除行功能,本文通過示例代碼給大家介紹的非常詳細(xì),感興趣的朋友跟隨小編一起看看吧
    2024-07-07
  • vue3?騰訊地圖設(shè)置簽到范圍并獲取經(jīng)緯度的實現(xiàn)代碼

    vue3?騰訊地圖設(shè)置簽到范圍并獲取經(jīng)緯度的實現(xiàn)代碼

    本文給大家介紹vue3?騰訊地圖設(shè)置簽到范圍并獲取經(jīng)緯度的實現(xiàn)代碼,本文通過示例代碼給大家介紹的非常詳細(xì),感興趣的朋友跟隨小編一起看看吧
    2022-05-05
  • 快速掌握Vue3.0中如何上手Vuex狀態(tài)管理

    快速掌握Vue3.0中如何上手Vuex狀態(tài)管理

    Vuex 是一個專為 Vue.js 應(yīng)用程序開發(fā)的狀態(tài)管理模式,本文給大家分享Vue3.0中快速上手Vuex狀態(tài)管理的方式,本文通過實例代碼講解的很詳細(xì),對大家學(xué)習(xí)Vuex狀態(tài)管理相關(guān)知識有很大的幫助,感興趣的朋友一起學(xué)習(xí)吧
    2021-05-05
  • vue3.0?setup中使用vue-router問題

    vue3.0?setup中使用vue-router問題

    這篇文章主要介紹了vue3.0?setup中使用vue-router問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-10-10

最新評論