vue3中獲取ref元素的幾種方式總結(jié)
vue3獲取ref元素方式
1. 原生js獲取dom元素:
document.querySelector(選擇器) document.getElementById(id選擇器) document.getElementsByClassName(class選擇器)
2. ref獲取單個dom元素:
<template> ? ?<div ref='divDom'></div>? </template> <script setup> import { ref} from 'vue' const divDom = ref(null); onMounted(()=>{ ? ? console.log('獲取dom元素',divDom) })
3. ref獲取v-for循環(huán)中的dom元素:
<template> ? ?<div ref='getDivDom' v-for="item in list" :data-id="item.id"></div>? </template> <script setup> import { ref} from 'vue' const divDomList = ref(new Map()); const getDivDom = el=>{ ? ? if(el){ ? ? ? ? divDomList.set(el.dataset['id'],el)? ? ? } } // const el =divDomList.get(id) // 根據(jù)list數(shù)據(jù)中的id值 獲取對應的dom元素?
4. 在swiper中獲取swiper的dom元素:
<template> ? ?<swiper @swiper='getSwiper'></swiper >? </template> <script setup> import swiper from 'swiper' import { ref} from 'vue' const swiperDom= ref(null); const getSwiper= el=>{ ? ? swiperDom.value = el; }
vue3中ref獲取dom(包含for循環(huán))
如何在Vue3中通過ref獲取dom元素,這里說一下我遇到的情況和使用方式
情況一:只是單純的獲取某個dom元素
這種情況比較簡單,直接在js中通過ref定義一個和html中元素上ref相同名字的變量即可
例子:
<template> <div class="box" ref="boxRef"> box </div> </template> <script lang="ts" setup> import { onMounted, ref } from 'vue'; const boxRef = ref<HTMLElement>() // 這里定義一個和div中ref名字一樣的變量名即可 onMounted(() => { if (boxRef.value) { console.log(boxRef.value) } }) </script>
情況二:在for循環(huán)中獲取dom元素
這種情況下,我們可以通過動態(tài)設置ref的形式進行設置ref,這樣我們就可以獲取到一個ref的數(shù)組
例子:
<template> <div class="box"> <div v-for="item in 10" :key="item" :ref="setBoxRef"> box </div> </div> </template> <script lang="ts" setup> import { onMounted, ref } from 'vue'; const boxRefs = ref<HTMLElement[]>([]) const setBoxRef = (el: any) => { if (el) { boxRefs.value.push(el) } } onMounted(() => { console.log(boxRefs.value) }) </script>
情況三:獲取ref中的ref
這種情況我們不能像Vue2的方式一樣通過refs.refs
或者.children
的形式,因為你會發(fā)現(xiàn),這些方法都不能用了
所以要解決這個問題,我們需要借助Vue3提供的新的方法getCurrentInstance
需要注意的是,getCurrentInstance
只能在setup
或者生命周期
中使用才有效
具體參考官方文檔:https://v3.cn.vuejs.org/api/composition-api.html#getcurrentinstance
例子:
<template> <div class="box" ref="boxRef"> <div ref="boxInnerRef"> <div ref="innerRef"> innerRef </div> </div> </div> </template> <script lang="ts" setup> import { onMounted, getCurrentInstance } from 'vue'; onMounted(() => { const instance = getCurrentInstance() if (instance) { console.log(instance.refs) } }) </script>
我們可以看到,不管多少層的ref
,Vue3都給處理成了一層的Object形式,我們就能很方便的拿到自己需要的dom元素了
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
react中value與defaultValue的區(qū)別及說明
這篇文章主要介紹了react中value與defaultValue的區(qū)別及說明,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-05-05React+TypeScript項目中使用CodeMirror的步驟
CodeMirror被廣泛應用于許多Web應用程序和開發(fā)工具,之前做需求用到過codeMirror這個工具,覺得還不錯,功能很強大,所以記錄一下改工具的基礎用法,對React+TypeScript項目中使用CodeMirror的步驟感興趣的朋友跟隨小編一起看看吧2023-07-07react的ui庫antd中form表單使用SelectTree反顯問題及解決
這篇文章主要介紹了react的ui庫antd中form表單使用SelectTree反顯問題及解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-01-01React-router?v6在Class組件和非組件代碼中的正確使用
這篇文章主要介紹了React-router?v6在Class組件和非組件代碼中的正確使用方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-03-03用react-redux實現(xiàn)react組件之間數(shù)據(jù)共享的方法
這篇文章主要介紹了用react-redux實現(xiàn)react組件之間數(shù)據(jù)共享的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-06-06react+antd4實現(xiàn)優(yōu)化大批量接口請求
這篇文章主要為大家詳細介紹了如何使用react hooks + antd4實現(xiàn)大批量接口請求的前端優(yōu)化,文中的示例代碼講解詳細,感興趣的小伙伴可以了解下2024-02-02React videojs 實現(xiàn)自定義組件(視頻畫質(zhì)/清晰度切換) 的操作代碼
最近使用videojs作為視頻處理第三方庫,用來對接m3u8視頻類型,這里總結(jié)一下自定義組件遇到的問題及實現(xiàn),感興趣的朋友跟隨小編一起看看吧2023-08-08