您好,登錄后才能下訂單哦!
這篇文章主要介紹“Vue3中操作dom的方式有哪些”,在日常操作中,相信很多人在Vue3中操作dom的方式有哪些問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”Vue3中操作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>
通過對 div
元素添加 ref
屬性,為了獲取到這個元素,我們聲明了一個與 ref
屬性名稱相同的變量,然后通過 [變量名].value
的形式即可獲取該 div
元素。
單一 dom
元素或者個數較少的場景
<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
屬性,并聲明一個與 ref
屬性名稱相同的變量 list
,此時通過 list.value
會獲得包含子元素的 dom
對象。此時可以通過 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()
通過 v-for
循環生成的固定數量元素的場景。
<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>
通過對子組件添加 ref
屬性,并聲明一個與 ref
屬性名稱相同的變量 childRef
,此時通過 emit
將 childRef.value
作為一個 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>
多個頁面都可能有操作組件 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> <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
循環調用 setRefAction
方法,該方法會默認接收一個 el
參數,這個參數就是我們需要獲取的 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>
此時可以通過 state.refList[index]
的形式獲取子元素 dom
。
通過 v-for
循環生成的不固定數量或者多種元素的場景。
<template> <div class="ref-container"> <p>通過:ref將dom引用放到數組中</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>
1,獲取dom的ref元素名稱,要對應暴露的名稱,不然會出現無效的dom報錯,也就是拿到的是null
2,在setup中,使用ref(null)獲取dom
3,不能直接在setup里面拿到dom的值,因為setup對應的生命周期是created,所以必須在后續的生命周期鉤子里面拿到,比如onMounted
到此,關于“Vue3中操作dom的方式有哪些”的學習就結束了,希望能夠解決大家的疑惑。理論與實踐的搭配能更好的幫助大家學習,快去試試吧!若想繼續學習更多相關知識,請繼續關注億速云網站,小編會繼續努力為大家帶來更多實用的文章!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。