VueHow to operate dom? The following article will introduce to you four ways to operate dom in Vue3. I hope it will be helpful to you!
Recently, product managers have put forward a lot of requirements for user experience optimization, which involves a lot of DOM operations.
Xiao Zhang: "Lao Tie, it used to be quite simple to operate the DOM when developing a Vue2 project. Now I'm developing a Vue3 project and suddenly feel confused!"
Me: "It's okay, the principle They are almost the same, you should be fine if you check the information!"
This is a summary of several common ways to operate DOM in Vue3! (Learning video sharing: vue video tutorial)
<template> <div> <div ref="sectionRef"></div> </div> </template> <script setup> import {ref} from 'vue' const sectionRef = ref() </script>
By adding the ref attribute to the div element, In order to obtain this element, we declare a variable sectionRef with the same name as the ref attribute, and then we can obtain the div element in the form of sectionRef.value.
Single DOM element or a small number of scenarios
<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()
Add the ref attribute to the parent element and declare a ref attribute name The same variable listRef, at this time, the dom object containing the child elements will be obtained through listRef.value
At this time, the children can be obtained through listRef.value.children[index] Element dom
Scenarios with a fixed number of elements generated through v-for loop
<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>
Call the setRefAction method through:ref loop, which will receive it by default An el parameter, this parameter is the div element we need to get
At this time, the sub-element dom
Scenes with an unfixed number or multiple elements generated through v-for loop
<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>
Add the ref attribute to the subcomponent and declare a variable cellRef with the same name as the ref attribute , at this time, you can pass cellRef.value as a dom reference through emit
Possible for multiple pages There are scenarios for operating component 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>
[Related video tutorial recommendations: vuejs introductory tutorial、Getting started with web front-end】
The above is the detailed content of How does Vue3 operate dom? Four ways to introduce. For more information, please follow other related articles on the PHP Chinese website!