Vue如何操作dom?以下這篇文章為大家介紹Vue3中操作dom的四種方式,希望能給大家有幫助!
最近產品經理提出了許多使用者體驗優化的需求,涉及很多dom的操作。
小張:「老鐵,本來開發Vue2專案操作dom挺簡單的,現在開發vue3項目,突然感覺一頭霧水!」
我:「沒事,原理都差不多,查資料應該沒問題的!」
至此將Vue3中dom操作常見的幾種方式總結一下! (學習影片分享:vue影片教學)
<template> <div> <div ref="sectionRef"></div> </div> </template> <script setup> import {ref} from 'vue' const sectionRef = ref() </script>
透過對div元素加入了ref屬性,為了取得這個元素,我們宣告了一個與ref屬性名稱相同的變數sectionRef,然後我們透過sectionRef.value 的形式即可取得該div元素。
單一dom元素或數量較少的場景
<template> <div> <p>通过ref直接拿到dom</p> <div ref="sectionRef"></div> <button @click="higherAction">变高</button> </div> </template> <script setup> import {ref} from 'vue' const sectionRef = ref() let height = 100; const higherAction = () => { height += 50; sectionRef.value.style = `height: ${height}px`; } </script> <style 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>
<template> <div> <div ref="listRef"> <div @click="higherAction(index)" v-for="(item, index) in state.list" :key="index"> <span>{{item}}</span> </div> </div> </div> </template> <script setup> import { ref, reactive } from 'vue' const listRef = ref()
透過對父元素新增了ref屬性,並宣告了一個與ref屬性名稱相同的變數listRef,此時透過listRef.value會得到包含子元素的dom物件
此時可以透過listRef.value.children[index]的形式取得子元素dom
透過v-for迴圈產生的固定數量元素的場景
<template> <div> <p>通过父容器遍历拿到dom</p> <div ref="listRef"> <div @click="higherAction(index)" v-for="(item, index) in state.list" :key="index"> <span>{{item}}</span> </div> </div> </div> </template> <script setup> import { ref, reactive } from 'vue' const listRef = 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 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>
<template> <div> <div> <div :ref="setRefAction" @click="higherAction(index)" v-for="(item, index) in state.list" :key="index"> <span>{{item}}</span> </div> </div> </div> </template> <script 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>
透過:ref循環呼叫setRefAction方法,該方法會預設接收一個el參數,這個參數就是我們需要取得的div元素
此時可以透過state.refList[index]的形式取得子元素dom
透過v-for迴圈產生的不固定數量或多種元素的場景
<template> <div> <p>通过:ref将dom引用放到数组中</p> <div> <div :ref="setRefAction" @click="higherAction(index)" v-for="(item, index) in state.list" :key="index"> <span>{{item}}</span> </div> </div> </div> </template> <script 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 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>
<template> <div ref="cellRef" @click="cellAction"> <span>{{item}}</span> </div> </template> <script setup> import {ref} from 'vue'; const props = defineProps({ item: Number }) const emit = defineEmits(['cellTap']); const cellRef = ref(); const cellAction = () => { emit('cellTap', cellRef.value); } </script>
透過對子元件加入了ref屬性,並宣告了一個與ref屬性名稱相同的變數cellRef ,此時可以透過emit將cellRef.value作為一個dom參考傳遞出去
<template> <div ref="cellRef" @click="cellAction"> <span>{{item}}</span> </div> </template> <script setup> import {ref} from 'vue'; const props = defineProps({ item: Number }) const emit = defineEmits(['cellTap']); const cellRef = ref(); const cellAction = () => { emit('cellTap', cellRef.value); } </script>
<template> <div> <p>通过子组件emit传递ref</p> <div> <Cell :item="item" @cellTap="cellTapHandler" v-for="(item, index) in state.list" :key="index"> </Cell> </div> </div> </template> <script 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 scoped> .demo2-container { width: 100%; height: 100%; .list-section { width: 200px; } } </style>
以上是Vue3如何操作dom?四種方式介紹的詳細內容。更多資訊請關注PHP中文網其他相關文章!