您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

Vue3如何操作dom?四种方式介绍

2024/4/6 20:20:50发布32次查看
vue如何操作dom?下面本篇文章给大家介绍一下vue3中操作dom的四种方式,希望给大家有所帮助!
最近产品经理提出了很多用户体验优化的需求,涉及到很多dom的操作。
小张:“老铁,本来开发vue2项目操作dom挺简单的,现在开发vue3项目,突然感觉一头雾水!”
我:“没事,原理都差不多,查查资料应该没问题的!”
至此将vue3中dom操作常见的几种方式总结一下!(学习视频分享:vue视频教程)
通过ref直接拿到dom引用<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>
通过父容器的ref遍历拿到dom引用<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>
通过:ref将dom引用放到数组中<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>
通过子组件emit传递ref<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引用传递出去
适用场景多个页面都可能有操作组件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><style 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> <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>
【相关视频教程推荐:vuejs入门教程、web前端入门】
以上就是vue3如何操作dom?四种方式介绍的详细内容。
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product