Vue3中操作dom的四種方式保姆級(jí)教程(推薦!)
前言
最近主管提出了許多優(yōu)化用戶體驗(yàn)的要求,其中很多涉及 dom 操作。本文將 Vue3 中常見的 dom 操作總結(jié)了一下。覺得文章不錯(cuò)、或?qū)ψ约洪_發(fā)有所幫助!
一、通過 ref 拿到 dom 的引用
<template>
<div class="ref-container">
<div ref="sectionRef" class="ref-section"></div>
</div>
</template>
<script lang="ts" setup>
import { ref } from 'vue'
const sectionRef = ref()
</script>
通過對(duì) div 元素添加 ref 屬性,為了獲取到這個(gè)元素,我們聲明了一個(gè)與 ref 屬性名稱相同的變量,然后通過 [變量名].value 的形式即可獲取該 div 元素。
適用場(chǎng)景
- 單一
dom元素或者個(gè)數(shù)較少的場(chǎng)景
示例代碼
<template>
<div class="ref-container">
<p>通過 ref 直接拿到 dom</p>
<div ref="sectionRef" class="ref-section"></div>
<button @click="action" class="btn">變高</button>
</div>
</template>
<script lang="ts" setup>
import { ref } from 'vue'
const sectionRef = ref()
let height = 100;
const action= () => {
height += 50;
sectionRef.value.style = `height: ${height}px`;
}
</script>
<style lang="scss" scoped>
.demo1-container {
width: 100%;
height: 100%;
.ref-section {
width: 200px;
height: 100px;
background-color: pink;
transition: all .5s ease-in-out;
}
.btn {
width: 200px;
height: 50px;
background-color: gray;
color: #fff;
margin-top: 100px;
}
}
</style>
二、通過父容器的 ref 遍歷拿到 dom 引用
通過對(duì)父元素添加 ref 屬性,并聲明一個(gè)與 ref 屬性名稱相同的變量 list,此時(shí)通過 list.value 會(huì)獲得包含子元素的 dom 對(duì)象。此時(shí)可以通過 list.value.children[index] 的形式獲取子元素 dom。
<template>
<div class="ref-container">
<div ref="list" 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 lang="ts" setup>
import { ref, reactive } from 'vue'
const list = ref()
適用場(chǎng)景
- 通過
v-for循環(huán)生成的固定數(shù)量元素的場(chǎng)景。
示例代碼
<template>
<div class="ref-container">
<p>通過父容器遍歷拿到dom</p>
<div ref="list" 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 lang="ts" setup>
import { ref, reactive } from 'vue'
const list = ref()
const state = reactive({
list: [1, 2, 3, 4, 5, 6, 7, 8]
})
const higherAction = (index: number) => {
let height = listRef.value.children[index].style.height ? listRef.value.children[index].style.height : '20px';
height = Number(height.replace('px', ''));
listRef.value.children[index].style = `height: ${height + 20}px`;
}
</script>
<style lang="scss" scoped>
.demo2-container {
width: 100%;
height: 100%;
.list-section {
width: 200px;
.list-item {
width: 200px;
height: 20px;
background-color: pink;
color: #333;
transition: all .5s ease-in-out;
display: flex;
justify-content: center;
align-items: center;
}
}
}
</style>
三、通過子組件 emit 傳遞 ref
通過對(duì)子組件添加 ref 屬性,并聲明一個(gè)與 ref 屬性名稱相同的變量 childRef,此時(shí)通過 emit 將 childRef.value 作為一個(gè) dom 引用傳遞出去。
<template>
<div ref="childRef" @click="cellAction" class="cell-item">
<span>{{item}}</span>
</div>
</template>
<script lang="ts" setup>
import { ref } from 'vue'
const props = defineProps({
item: Number
})
const emit = defineEmits(['cellTap']);
const childRef = ref();
const cellAction = () => {
emit('cellTap', childRef.value);
}
</script>
適用場(chǎng)景
- 多個(gè)頁(yè)面都可能有操作組件
dom的場(chǎng)景
示例代碼
<template>
<div ref="childRef" @click="cellAction" class="cell-item">
<span>{{item}}</span>
</div>
</template>
<script lang="ts" setup>
import { ref } from 'vue'
const props = defineProps({
item: Number
})
const emit = defineEmits(['cellTap']);
const childRef = ref()
const cellAction = () => {
emit('cellTap', childRef.value);
}
</script>
<style lang="scss" scoped>
.cell-item {
width: 200px;
height: 20px;
background-color: pink;
color: #333;
transition: all .5s ease-in-out;
display: flex;
justify-content: center;
align-items: center;
}
</style>
<template>
<div class="ref-container">
<p>通過子組件emit傳遞ref</p>
<div class="list-section">
<Cell :item="item" @cellTap="cellTapHandler" v-for="(item, index) in state.list" :key="index">
</Cell>
</div>
</div>
</template>
<script lang="ts" setup>
import { reactive } from 'vue'
import Cell from '@/components/Cell.vue'
const state = reactive({
list: [1, 2, 3, 4, 5, 6, 7],
refList: [] as Array<any>
})
const cellTapHandler = (el: any) => {
let height = el.style.height ? el.style.height : '20px';
height = Number(height.replace('px', ''));
el.style = `height: ${height + 20}px`;
}
</script>
<style lang="scss" scoped>
.demo2-container {
width: 100%;
height: 100%;
.list-section {
width: 200px;
}
}
</style>
四、通過 :ref 將 dom 引用放到數(shù)組中
通過 :ref 循環(huán)調(diào)用 setRefAction 方法,該方法會(huì)默認(rèn)接收一個(gè) el 參數(shù),這個(gè)參數(shù)就是我們需要獲取的 div 元素。
<template>
<div class="ref-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 lang="ts" setup>
import { reactive } from 'vue'
const state = reactive({
list: [1, 2, 3, 4, 5, 6, 7],
refList: [] as Array<any>
})
const setRefAction = (el: any) => {
state.refList.push(el);
}
</script>
此時(shí)可以通過 state.refList[index] 的形式獲取子元素 dom。
適用場(chǎng)景
- 通過
v-for循環(huán)生成的不固定數(shù)量或者多種元素的場(chǎng)景。
示例代碼
<template>
<div class="ref-container">
<p>通過:ref將dom引用放到數(shù)組中</p>
<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 lang="ts" setup>
import { reactive } from 'vue'
const state = reactive({
list: [1, 2, 3, 4, 5, 6, 7],
refList: [] as Array<any>
})
const higherAction = (index: number) => {
let height = state.refList[index].style.height ? state.refList[index].style.height : '20px';
height = Number(height.replace('px', ''));
state.refList[index].style = `height: ${height + 20}px`;
console.log(state.refList[index]);
}
const setRefAction = (el: any) => {
state.refList.push(el);
}
</script>
<style lang="scss" scoped>
.demo2-container {
width: 100%;
height: 100%;
.list-section {
width: 200px;
.list-item {
width: 200px;
height: 20px;
background-color: pink;
color: #333;
transition: all .5s ease-in-out;
display: flex;
justify-content: center;
align-items: center;
}
}
}
</style>
附:在vue3中獲取dom,有幾點(diǎn)需要注意
1,獲取dom的ref元素名稱,要對(duì)應(yīng)暴露的名稱,不然會(huì)出現(xiàn)無效的dom報(bào)錯(cuò),也就是拿到的是null
2,在setup中,使用ref(null)獲取dom
3,不能直接在setup里面拿到dom的值,因?yàn)閟etup對(duì)應(yīng)的生命周期是created,所以必須在后續(xù)的生命周期鉤子里面拿到,比如onMounted
總結(jié)
經(jīng)過了不斷探索終于完成了,事后想了一下還是自己的本事不到家,以后還是需要不斷提升自己的能力 ~ ~ ~
到此這篇關(guān)于Vue3中操作dom的四種方式的文章就介紹到這了,更多相關(guān)Vue3操作dom方式內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Vue組件高級(jí)通訊之$children與$parent
這篇文章主要為大家介紹了Vue組件高級(jí)通訊之$children與$parent,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-06-06
Vue路由傳參頁(yè)面刷新后參數(shù)丟失原因和解決辦法
這幾天在開發(fā)中遇見的一個(gè)關(guān)于路由傳參后,頁(yè)面刷新數(shù)據(jù)丟失的問題,下面這篇文章主要給大家介紹了關(guān)于Vue路由傳參頁(yè)面刷新后參數(shù)丟失原因和解決辦法,需要的朋友可以參考下2022-12-12
vue-admin-template配置快捷導(dǎo)航的代碼(標(biāo)簽導(dǎo)航欄)
這篇文章主要介紹了vue-admin-template配置快捷導(dǎo)航的方法(標(biāo)簽導(dǎo)航欄),本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-09-09
如何搭建一個(gè)完整的Vue3.0+ts的項(xiàng)目步驟
這篇文章主要介紹了如何搭建一個(gè)完整的Vue3.0+ts的項(xiàng)目步驟,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-10-10
Vue項(xiàng)目使用Websocket大文件FileReader()切片上傳實(shí)例
這篇文章主要介紹了Vue項(xiàng)目使用Websocket大文件FileReader()切片上傳實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-10-10
vue?watch報(bào)錯(cuò):Error?in?callback?for?watcher?"xxx&qu
這篇文章主要給大家介紹了關(guān)于vue?watch報(bào)錯(cuò):Error?in?callback?for?watcher?“xxx“:“TypeError:Cannot?read?properties?of?undefined的解決方法,需要的朋友可以參考下2023-03-03
vue+element-ui+axios實(shí)現(xiàn)圖片上傳
這篇文章主要為大家詳細(xì)介紹了vue+element-ui+axios實(shí)現(xiàn)圖片上傳,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-08-08

