vue3?setup語(yǔ)法糖中獲取slot插槽的dom對(duì)象代碼示例
前言
最近使用vue3開(kāi)發(fā)項(xiàng)目,需要封裝一個(gè)無(wú)限滾動(dòng)的組件,使用scroll組件內(nèi)置插槽接受模板的方式,所以需要在scroll組件內(nèi)獲取到模板渲染后dom元素的寬高。
但是setup語(yǔ)法糖是組件生命周期的beforeCreate和created中,而且經(jīng)過(guò)測(cè)試,在mounted函數(shù)中的el屬性也是null,所以得出結(jié)論模板的slot.default無(wú)法直接獲取, 必須通過(guò) render 方式對(duì) slot 的 vnode 進(jìn)行渲染,然后在 render 組件中的 mounted 方法中才能獲取到。如下面的例子
容器組件 ScrollView
//ScrollView.vue scroll容器組件 <script setup lang="ts"> import { ref, useSlots } from 'vue' import createSlot from '../vnode/createSlot' const slot = useSlots() // 這里獲取到的是默認(rèn)插槽的vnode,但拿不到對(duì)應(yīng)的dom實(shí)例 const defaultSlot = slots.default && slots.default()[0] // 自定義template 內(nèi)容mounted事件 const mountedCallFun = (args)=> { console.log('mounted', args) } // 自定義template 內(nèi)容updated事件 const updatedCallFun = (args)=> { console.log(args) } // 自定義template 內(nèi)容unMounted卸載事件 const unmountedCallFun = (args)=> { console.log(args) } const mySlot = createSlot({mountedCallFun, updatedCallFun, unmountedCallFun}) onMounted(() => { // 即使在該組件的mounted鉤子中,這個(gè)defaultSlot的$el依然為null console.log('defaultSlot', defaultSlot) }) </script> <template> <div> <mySlot :vnode="defaultSlot"></mySlot> </div> </template>
render函數(shù)渲染slot內(nèi)容的工廠函數(shù) createSlots.ts
通過(guò) vue官方提供的 defineComponent創(chuàng)建一個(gè)組件裝載 scrollView組件中的 插槽內(nèi)容:
//createSlots.ts import { defineComponent, h } from "vue" type CallFun = (vnodeEl: HTMLElement) => void type Funs = Record<'mountedCallFun'| 'updatedCallFun'| 'unmountedCallFun', CallFun> export default ({mountedCallFun, updatedCallFun, unmountedCallFun}: Funs) => { return defineComponent({ props: ['vnode'], setup(props, ctx){ console.log(props, ctx) }, mounted() { // console.log(this.$el) mountedCallFun(this.$el) }, render(props: any, ctx: any) { console.log(props, ctx) return props.vnode } }) }
測(cè)試使用 ScrollView組件
<script setup lang="ts"> import Text from '../components/text.vue' import ScrollView from '../components/ScrollView.vue' </script> <template> <div> <ScrollView> <h2 >測(cè)試使用 ScrollView組件測(cè)試使用 ScrollView組件</h2> <h2 >測(cè)試使用 ScrollView組件測(cè)試使用 ScrollView組件</h2> <h2 >測(cè)試使用 ScrollView組件測(cè)試使用 ScrollView組件</h2> <h2 >測(cè)試使用 ScrollView組件測(cè)試使用 ScrollView組件</h2> </ScrollView> </div> </template>
總結(jié)
到此這篇關(guān)于vue3 setup語(yǔ)法糖中獲取slot插槽的dom對(duì)象的文章就介紹到這了,更多相關(guān)vue3獲取slot插槽dom對(duì)象內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
基于Vue?+?ElementUI實(shí)現(xiàn)可編輯表格及校驗(yàn)
這篇文章主要給大家介紹了基于Vue?+?ElementUI?實(shí)現(xiàn)可編輯表格及校驗(yàn),文中有詳細(xì)的代碼講解和實(shí)現(xiàn)思路,講解的非常詳細(xì),有需要的朋友可以參考下2023-08-08Vue圖片瀏覽組件v-viewer用法分析【支持旋轉(zhuǎn)、縮放、翻轉(zhuǎn)等操作】
這篇文章主要介紹了Vue圖片瀏覽組件v-viewer用法,結(jié)合實(shí)例形式分析了v-viewer的基本功能與使用方法,包括旋轉(zhuǎn)、縮放、翻轉(zhuǎn)等操作技巧,需要的朋友可以參考下2019-11-11vue使用recorder-core.js實(shí)現(xiàn)錄音功能
這篇文章主要介紹了vue如何使用recorder-core.js實(shí)現(xiàn)錄音功能,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-07-07Vue3如何自定義v-permission權(quán)限指令
這篇文章主要為大家詳細(xì)介紹了Vue3如何編寫(xiě)一個(gè)?v-permission?指令來(lái)根據(jù)用戶權(quán)限動(dòng)態(tài)控制元素的渲染,感興趣的小伙伴可以參考一下2024-12-12vue項(xiàng)目中axios請(qǐng)求網(wǎng)絡(luò)接口封裝的示例代碼
這篇文章主要介紹了vue項(xiàng)目中axios請(qǐng)求網(wǎng)絡(luò)接口封裝的示例代碼,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-12-12Vue3 setup語(yǔ)法糖銷(xiāo)毀一個(gè)或多個(gè)定時(shí)器(setTimeout/setInterval)
這篇文章主要給大家介紹了關(guān)于Vue3 setup語(yǔ)法糖銷(xiāo)毀一個(gè)或多個(gè)定時(shí)器(setTimeout/setInterval)的相關(guān)資料,vue是單頁(yè)面應(yīng)用,路由切換后,定時(shí)器并不會(huì)自動(dòng)關(guān)閉,需要手動(dòng)清除,當(dāng)頁(yè)面被銷(xiāo)毀時(shí),清除定時(shí)器即可,需要的朋友可以參考下2023-10-10