ホームページ  >  記事  >  ウェブフロントエンド  >  Vue3 はどのように dom を操作するのでしょうか? 4つの導入方法

Vue3 はどのように dom を操作するのでしょうか? 4つの導入方法

青灯夜游
青灯夜游転載
2022-10-28 19:29:513445ブラウズ

Vuedomの操作方法は?以下の記事ではVue3でdomを操作する4つの方法を紹介しますので、ご参考になれば幸いです。

Vue3 はどのように dom を操作するのでしょうか? 4つの導入方法

最近、プロダクト マネージャーはユーザー エクスペリエンスの最適化に関する多くの要件を提案していますが、これには多くの DOM 操作が含まれます。

Xiao Zhang: 「Lao Tie、以前は Vue2 プロジェクトを開発するときに DOM を操作するのは非常に簡単でした。今は Vue3 プロジェクトを開発しているのですが、突然混乱を感じます!」

私:「大丈夫です、原理はほぼ同じです、情報を確認すれば大丈夫です!」

これは、Vue3 で DOM を操作する一般的な方法をいくつかまとめたものです。 (学習ビデオ共有: vue ビデオ チュートリアル )

ref を通じて dom 参照を直接取得します

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

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

div 要素に ref 属性を追加します。 , この要素を取得するには、ref属性と同じ名前の変数sectionRefを宣言すると、sectionRef.valueの形式でdiv要素を取得できます。

適用可能なシナリオ

単一の DOM 要素または少数のシナリオ

Vue3 はどのように dom を操作するのでしょうか? 4つの導入方法

サンプル コード

<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>

親コンテナの 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 &#39;vue&#39;
const listRef = ref()

親要素に ref 属性を追加し、ref 属性名を宣言する 同じこのとき、子要素を含む dom オブジェクトは listRef.value を通じて取得されます。

Vue3 はどのように dom を操作するのでしょうか? 4つの導入方法

このとき、子要素は listRef.value を通じて取得できます。 Children[index] 要素 dom

適用可能なシナリオ

v-for ループを通じて生成される固定数の要素を含むシナリオ

Vue3 はどのように dom を操作するのでしょうか? 4つの導入方法

サンプル コード

<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>

: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 &#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>

:ref ループを通じて setRefAction メソッドを呼び出します。デフォルトでそれを受け取ります。 el パラメータ。このパラメータは取得する必要がある div 要素です。

Vue3 はどのように dom を操作するのでしょうか? 4つの導入方法

#現時点では、サブ要素 dom

state.refList[index] の形式で取得可能

適用可能なシナリオ

v-for ループを通じて生成される不定の数または複数の要素を持つシーン

Vue3 はどのように dom を操作するのでしょうか? 4つの導入方法

コード例
<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>

サブコンポーネントに ref を渡す 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>

サブコンポーネントに ref 属性を追加し、変数 cellRef を ref 属性と同じ名前にします。この時点で、emit を通じて cellRef.value を dom 参照として渡すことができます

Vue3 はどのように dom を操作するのでしょうか? 4つの導入方法

適用可能なシナリオ

複数ページ可能コンポーネントdomを操作するシナリオあり

Vue3 はどのように dom を操作するのでしょうか? 4つの導入方法

サンプルコード
<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>

<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>

[関連動画]チュートリアルの推奨事項:

vuejs 入門チュートリアルWeb フロントエンドの入門]

以上がVue3 はどのように dom を操作するのでしょうか? 4つの導入方法の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

声明:
この記事はjuejin.cnで複製されています。侵害がある場合は、admin@php.cn までご連絡ください。