Home > Web Front-end > Vue.js > How does Vue3 operate dom? Four ways to introduce

How does Vue3 operate dom? Four ways to introduce

青灯夜游
Release: 2022-10-28 19:29:51
forward
3609 people have browsed it

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!

How does Vue3 operate dom? Four ways to introduce

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)

Get the dom reference directly through ref

<template>
    <div>
        <div ref="sectionRef"></div>
    </div>
</template>

<script setup>
import {ref} from &#39;vue&#39;
const sectionRef = ref()
</script>
Copy after login

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.

Applicable scenarios

Single DOM element or a small number of scenarios

How does Vue3 operate dom? Four ways to introduce

Example code

<template>
    <div>
        <p>通过ref直接拿到dom</p>
        <div ref="sectionRef"></div>
        <button @click="higherAction">变高</button>
    </div>
</template>

<script setup>
import {ref} from &#39;vue&#39;
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>
Copy after login

Get the dom reference through the ref traversal of the parent container

<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 &#39;vue&#39;
const listRef = ref()
Copy after login

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

How does Vue3 operate dom? Four ways to introduce

At this time, the children can be obtained through listRef.value.children[index] Element dom

Applicable scenarios

Scenarios with a fixed number of elements generated through v-for loop

How does Vue3 operate dom? Four ways to introduce

Sample code

<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 &#39;vue&#39;
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 : &#39;20px&#39;;
    height = Number(height.replace(&#39;px&#39;, &#39;&#39;));
    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>
Copy after login

Put the dom reference into the array through:ref

<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 &#39;vue&#39;

const state = reactive({
    list: [1, 2, 3, 4, 5, 6, 7],
    refList: [] as Array<any>
})

const setRefAction = (el: any) => {
    state.refList.push(el);
}
</script>
Copy after login

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

How does Vue3 operate dom? Four ways to introduce

At this time, the sub-element dom

can be obtained in the form of state.refList[index] Applicable scenarios

Scenes with an unfixed number or multiple elements generated through v-for loop

How does Vue3 operate dom? Four ways to introduce

Example code

<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 &#39;vue&#39;

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 : &#39;20px&#39;;
    height = Number(height.replace(&#39;px&#39;, &#39;&#39;));
    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>
Copy after login

Pass ref through subcomponent emit

<template>
    <div ref="cellRef" @click="cellAction">
        <span>{{item}}</span>
    </div>
</template>

<script setup>
import {ref} from &#39;vue&#39;;

const props = defineProps({
    item: Number
})
const emit = defineEmits([&#39;cellTap&#39;]);
const cellRef = ref();
const cellAction = () => {
    emit(&#39;cellTap&#39;, cellRef.value);
}
</script>
Copy after login

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

How does Vue3 operate dom? Four ways to introduce

Applicable scenarios

Possible for multiple pages There are scenarios for operating component dom

How does Vue3 operate dom? Four ways to introduce

Sample code

<template>
    <div ref="cellRef" @click="cellAction">
        <span>{{item}}</span>
    </div>
</template>

<script setup>
import {ref} from &#39;vue&#39;;

const props = defineProps({
    item: Number
})
const emit = defineEmits([&#39;cellTap&#39;]);
const cellRef = ref();
const cellAction = () => {
    emit(&#39;cellTap&#39;, cellRef.value);
}
</script>

Copy after login
<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 &#39;vue&#39;
import Cell from &#39;@/components/Cell.vue&#39;
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 : &#39;20px&#39;;
    height = Number(height.replace(&#39;px&#39;, &#39;&#39;));
    el.style = `height: ${height + 20}px`;
}
</script>

<style scoped>
.demo2-container {
    width: 100%;
    height: 100%;

    .list-section {
        width: 200px;
    }
}
</style>
Copy after login

[Related video tutorial recommendations: vuejs introductory tutorialGetting 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!

Related labels:
source:juejin.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template