最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Vue3中操作dom的四種方式總結(建議收藏!)

 更新時間:2022年12月29日 10:49:49   作者:瀟瀟夜雨丶  
VUE是通過傳遞一些配置給Vue對象和頁面中引用插值表達式來操作DOM的,下面這篇文章主要給大家介紹了關于Vue3中操作dom的四種方式總結,文中通過實例代碼介紹的非常詳細,需要的朋友可以參考下

前言

最近產品經理提出了很多用戶體驗優(yōu)化的需求,涉及到很多dom的操作。

小張:“老鐵,本來開發(fā)Vue2項目操作dom挺簡單的,現在開發(fā)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循環(huán)生成的固定數量元素的場景

示例代碼

<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循環(huán)調用setRefAction方法,該方法會默認接收一個el參數,這個參數就是我們需要獲取的div元素

此時可以通過state.refList[index]的形式獲取子元素dom

適用場景

通過v-for循環(huán)生成的不固定數量或者多種元素的場景

示例代碼

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

寫在最后

到此這篇關于Vue3中操作dom的四種方式總結的文章就介紹到這了,更多相關Vue3操作dom方式內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

  • vue在圖片上傳的時候壓縮圖片

    vue在圖片上傳的時候壓縮圖片

    這篇文章主要介紹了vue在圖片上傳的時候壓縮圖片,幫助大家緩解服務器壓力,提高程序性能,感興趣的朋友可以了解下
    2020-11-11
  • vue常用指令實現學生錄入系統(tǒng)的實戰(zhàn)

    vue常用指令實現學生錄入系統(tǒng)的實戰(zhàn)

    本文主要介紹了vue常用指令實現學生錄入系統(tǒng)的實戰(zhàn),文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-02-02
  • vue實現選項卡及選項卡切換效果

    vue實現選項卡及選項卡切換效果

    這篇文章主要介紹了vue實現選項卡選項卡切換效果,這里的Vue以單文件的形式引入,另外代碼在實現上會一步步的進行優(yōu)化。需要的朋友可以參考下
    2018-04-04
  • 關于vue設置環(huán)境變量全流程

    關于vue設置環(huán)境變量全流程

    這篇文章主要介紹了關于vue設置環(huán)境變量全流程,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-03-03
  • Vue3中watch監(jiān)聽的五種情況詳解

    Vue3中watch監(jiān)聽的五種情況詳解

    watch函數用于偵聽某個值的變化,當該值發(fā)生改變后,觸發(fā)對應的處理邏輯,本文將給大家介紹了Vue3中watch監(jiān)聽的五種情況,文中通過代碼示例講解的非常詳細,具有一定的參考價值,需要的朋友可以參考下
    2024-03-03
  • vue中的input框點擊后不聚焦問題

    vue中的input框點擊后不聚焦問題

    這篇文章主要介紹了vue中的input框點擊后不聚焦問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-04-04
  • 關于el-select組件設置默認值的實現方式

    關于el-select組件設置默認值的實現方式

    這篇文章主要介紹了關于el-select組件設置默認值的實現方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-09-09
  • 關于element ui中el-cascader的使用方式

    關于element ui中el-cascader的使用方式

    這篇文章主要介紹了關于element ui中el-cascader的使用方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-09-09
  • Vue腳手架編寫試卷頁面功能

    Vue腳手架編寫試卷頁面功能

    腳手架是一種用于快速開發(fā)Vue項目的系統(tǒng)架構,這篇文章主要介紹了Vue腳手架實現試卷頁面功能,通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-03-03
  • Vue Element-UI中el-table實現單選的示例代碼

    Vue Element-UI中el-table實現單選的示例代碼

    在element-ui中是為我們準備好了可直接使用的單選與多選屬性的,本文主要介紹了Vue Element-UI中el-table實現單選的示例代碼,具有一定的參考價值,感興趣的可以了解一下
    2023-12-12

最新評論

府谷县| 红安县| 正蓝旗| 贵港市| 高邮市| 渝中区| 金秀| 阳春市| 来凤县| 五大连池市| 宜良县| 江都市| 尚义县| 安溪县| 昌江| 平南县| 望谟县| 迁西县| 五台县| 岱山县| 阿城市| 航空| 镇安县| 浏阳市| 宝坻区| 陇西县| 泸州市| 白水县| 浙江省| 策勒县| 伊金霍洛旗| 阿拉善盟| 林周县| 福清市| 洪江市| 吉安县| 五家渠市| 遵义市| 禹城市| 涞源县| 韩城市|