前语
最近产品司理提出了很多用户体验优化的需求,涉及到很多dom的操作。
小张:“老铁,原本开发Vue2项目操作dom挺简略的,现在开发vue3项目,忽然感觉一头雾水!”
我:“没事,原理都差不多,查查材料应该没问题的!”
至此将Vue3中dom操作常见的几种方法总结一下!
觉得文章不错、或对自己开发有所协助,欢迎点赞保藏!❤❤❤
经过ref直接拿到dom引证
<template>
<div class="demo1-container">
<div ref="sectionRef" class="ref-section"></div>
</div>
</template>
<script setup lang="ts">
import {ref} from 'vue'
const sectionRef = ref()
</script>
经过对div元素添加了ref特点,为了获取到这个元素,咱们声明晰一个与ref特点称号相同的变量sectionRef,然后咱们经过 sectionRef.value 的方法即可获取该div元素。
适用场景
单一dom元素或者个数较少的场景
示例代码
<template>
<div class="demo1-container">
<p>经过ref直接拿到dom</p>
<div ref="sectionRef" class="ref-section"></div>
<button @click="higherAction" class="btn">变高</button>
</div>
</template>
<script setup lang="ts">
import {ref} from 'vue'
const sectionRef = ref()
let height = 100;
const higherAction = () => {
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引证
<template>
<div class="demo2-container">
<div ref="listRef" 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 setup lang="ts">
import { ref, reactive } from 'vue'
const listRef = ref()
经过对父元素添加了ref特点,并声明晰一个与ref特点称号相同的变量listRef,此刻经过listRef.value会获得包含子元素的dom对象
此刻可以经过listRef.value.children[index]的方法获取子元素dom
适用场景
经过v-for循环生成的固定数量元素的场景
示例代码
<template>
<div class="demo2-container">
<p>经过父容器遍历拿到dom</p>
<div ref="listRef" 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 setup lang="ts">
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 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将dom引证放到数组中
<template>
<div class="demo2-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 setup lang="ts">
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 class="demo2-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 setup lang="ts">
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>
经过子组件emit传递ref
<template>
<div ref="cellRef" @click="cellAction" class="cell-item">
<span>{{item}}</span>
</div>
</template>
<script setup lang="ts">
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" class="cell-item">
<span>{{item}}</span>
</div>
</template>
<script setup lang="ts">
import {ref} from 'vue';
const props = defineProps({
item: Number
})
const emit = defineEmits(['cellTap']);
const cellRef = ref();
const cellAction = () => {
emit('cellTap', cellRef.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="demo2-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 setup lang="ts">
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>
写在最终
引荐几个作者参与的开源项目,假如项目有协助到你,欢迎star!
一个简略的根据Vue3、TS、Vite、qiankun技能栈的后台管理项目
:www.xkxk.tech
一个根据Vue3、Vite的仿element UI的组件库项目
:ui.xkxk.tech
一个根据Vue3、Vite的炫酷大屏项目
:screen.xkxk.tech