vue3中使用ref獲取dom的操作代碼
前言
本期文章我將給大家?guī)淼氖莢ue3中用ref獲取dom的操作,ref在我們開發(fā)項目當(dāng)中很重要的,在 Vue 中使用 ref 可以提高代碼的可讀性和維護性,因為它直接標(biāo)識出了組件中需要操作的具體元素或組件實例。這使得團隊合作、代碼審查和后續(xù)功能更新更加高效??偨Y(jié)來說,ref 在 Vue 中是一個非常有用的工具,不僅使得操作 DOM 更加方便和直觀,還能夠提升組件間通信的靈活性和效率。正確使用 ref 可以幫助開發(fā)者更好地管理和操作 DOM 元素以及組件實例,從而實現(xiàn)復(fù)雜的前端交互和 UI 動效。接下來我就一一道來吧
通過ref直接拿到dom引用
<template>
<div class="demo1-container">
<div ref="sectionRef" class="ref-section">1111111</div>
</div>
</template>
<script setup lang="ts">
import {onMounted, ref} from 'vue'
const sectionRef = ref(null)
onMounted(() => {
console.log(sectionRef.value)
})
</script>
在這里我們要注意的是當(dāng)在div標(biāo)簽中用 ref="sectionRef",之后在聲明響應(yīng)式變量的時候,要用sectionRef命名,這個是一定要的,然后我們通過 sectionRef.value 的形式即可獲取該div元素。 讓我們看看結(jié)果吧

單一dom元素或者個數(shù)較少的場景
通過父容器的ref遍歷拿到dom引用
<template>
<div class="demo2-container">
<div ref="listRef" class="list-section">
<div @click="higherAction(index)" class="list-item" v-for="(item, index) in state.list" :key="index">
<span>{{item}}</span>
</div>
</div>
</div>
</template>
<script setup lang="ts">
import { ref, reactive, onMounted } from 'vue'
const listRef = ref()
const state = reactive({
list: ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10']
})
onMounted(()=>{
console.log(listRef.value)
})
</script>
<style scoped>
.demo2-container {
width: 200px;
height: 400px;
border: 1px solid #000;
overflow: auto;
}
</style>
通過對父元素添加了ref屬性,并聲明了一個與ref屬性名稱相同的變量listRef,此時通過listRef.value會獲得包含子元素的dom對象,然后我們就可以此時通過listRef.value.children[index]的形式獲取子元素dom

通過:ref將dom引用放到數(shù)組中
<template>
<div class="demo2-container">
<div class="list-section">
<div :ref="setRefAction" @click="higherAction(index)" class="list-item" v-for="(item, index) in state.list" :key="index">
<span>{{item}}</span>
</div>
</div>
</div>
</template>
<script setup lang="ts">
import { onMounted, reactive ,ref} from 'vue'
const state = reactive({
list: [1, 2, 3, 4, 5, 6, 7],
refList: []
})
const index = ref(null)
const setRefAction = (el: any) => {
state.refList.push(el);
}
const higherAction = (index) => {
console.log(state.refList[index])
}
onMounted( () => {
console.log(state.refList);
setRefAction(index)
})
</script>
<style scoped>
.demo2-container {
width: 200px;
height: 400px;
border: 1px solid #000;
overflow: auto;
}
.list-item {
background-color: pink;
border: 1px solid #000;
}
</style>
這種情況下,我們可以通過動態(tài)設(shè)置ref的形式進行設(shè)置ref,這樣我們就可以獲取到一個ref的數(shù)組,結(jié)果如下

當(dāng)我們點擊哪個就會獲取哪個的dom,這樣我們簡單多了
到此這篇關(guān)于vue3中使用ref獲取dom的操作代碼的文章就介紹到這了,更多相關(guān)vue3 ref獲取dom內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
使用element+vuedraggable實現(xiàn)圖片上傳拖拽排序
這篇文章主要為大家詳細介紹了使用element+vuedraggable實現(xiàn)圖片上傳拖拽排序,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-04-04
Vue3項目打包后部署到服務(wù)器 請求不到后臺接口解決方法
在本篇文章里小編給大家整理了關(guān)于Vue3項目打包后部署到服務(wù)器 請求不到后臺接口解決方法,有需要的朋友們可以參考下。2020-02-02
詳解vue中的父子傳值雙向綁定及數(shù)據(jù)更新問題
這篇文章主要介紹了vue中的父子傳值雙向綁定及數(shù)據(jù)更新問題,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2019-06-06
詳解如何解決vue開發(fā)請求數(shù)據(jù)跨域的問題(基于瀏覽器的配置解決)
這篇文章主要介紹了詳解如何解決vue開發(fā)請求數(shù)據(jù)跨域的問題(基于瀏覽器的配置解決),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-11-11
vue jsx 使用指南及vue.js 使用jsx語法的方法
這篇文章主要介紹了vue jsx 使用指南及vue.js 使用jsx語法的方法,非常不錯,具有參考借鑒價值,需要的朋友可以參考下2017-11-11
vue 音樂App QQ音樂搜索列表最新接口跨域設(shè)置方法
這篇文章主要介紹了vue 音樂App QQ音樂搜索列表最新接口跨域設(shè)置方法,本文通過實例代碼給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下2018-09-09

