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

Vue3中操作ref的四種使用方式示例代碼(建議收藏)

 更新時(shí)間:2023年04月17日 15:09:31   作者:瀟瀟夜雨丶  
這篇文章主要介紹了Vue3中操作ref的四種使用方式示例代碼(建議收藏),本文通過(guò)示例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下

前言

最近產(chǎn)品經(jīng)理提出了很多用戶(hù)體驗(yàn)優(yōu)化的需求,涉及到很多dom的操作。

小張:“老鐵,本來(lái)開(kāi)發(fā)Vue2項(xiàng)目操作dom挺簡(jiǎn)單的,現(xiàn)在開(kāi)發(fā)vue3項(xiàng)目,突然感覺(jué)一頭霧水!”

我:“沒(méi)事,原理都差不多,查查資料應(yīng)該沒(méi)問(wèn)題的!”

至此將Vue3中dom操作常見(jiàn)的幾種方式總結(jié)一下!

通過(guò)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>

通過(guò)對(duì)div元素添加了ref屬性,為了獲取到這個(gè)元素,我們聲明了一個(gè)與ref屬性名稱(chēng)相同的變量sectionRef,然后我們通過(guò) sectionRef.value 的形式即可獲取該div元素。

適用場(chǎng)景

單一dom元素或者個(gè)數(shù)較少的場(chǎng)景

demo1.gif

示例代碼

<template>
    <div class="demo1-container">
        <p>通過(guò)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>

通過(guò)父容器的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()

通過(guò)對(duì)父元素添加了ref屬性,并聲明了一個(gè)與ref屬性名稱(chēng)相同的變量listRef,此時(shí)通過(guò)listRef.value會(huì)獲得包含子元素的dom對(duì)象

微信圖片_20221015192615.png

此時(shí)可以通過(guò)listRef.value.children[index]的形式獲取子元素dom

適用場(chǎng)景

通過(guò)v-for循環(huán)生成的固定數(shù)量元素的場(chǎng)景

demo2.gif

示例代碼

<template>
    <div class="demo2-container">
        <p>通過(guò)父容器遍歷拿到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>

通過(guò):ref將dom引用放到數(shù)組中

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

通過(guò):ref循環(huán)調(diào)用setRefAction方法,該方法會(huì)默認(rèn)接收一個(gè)el參數(shù),這個(gè)參數(shù)就是我們需要獲取的div元素

微信圖片_20221015193242.png

此時(shí)可以通過(guò)state.refList[index]的形式獲取子元素dom

適用場(chǎng)景

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

demo3.gif

示例代碼

<template>
    <div class="demo2-container">
        <p>通過(guò):ref將dom引用放到數(shù)組中</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>

通過(guò)子組件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>

通過(guò)對(duì)子組件添加了ref屬性,并聲明了一個(gè)與ref屬性名稱(chēng)相同的變量cellRef,此時(shí)可以通過(guò)emit將cellRef.value作為一個(gè)dom引用傳遞出去

微信圖片_20221015193830.png

適用場(chǎng)景

多個(gè)頁(yè)面都可能有操作組件dom的場(chǎng)景

demo4.gif

示例代碼

<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>通過(guò)子組件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>

寫(xiě)在最后

推薦幾個(gè)作者參與的開(kāi)源項(xiàng)目,如果項(xiàng)目有幫助到你,歡迎star!

一個(gè)簡(jiǎn)單的基于Vue3、TS、Vite、qiankun技術(shù)棧的后臺(tái)管理項(xiàng)目:www.xkxk.tech

一個(gè)基于Vue3、Vite的仿element UI的組件庫(kù)項(xiàng)目:ui.xkxk.tech

一個(gè)基于Vue3、Vite的炫酷大屏項(xiàng)目:screen.xkxk.tech

到此這篇關(guān)于Vue3中操作ref的四種使用方式示例代碼(建議收藏)的文章就介紹到這了,更多相關(guān)Vue3操作ref內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Vue項(xiàng)目如何安裝引入使用Vant

    Vue項(xiàng)目如何安裝引入使用Vant

    Vant是一個(gè)專(zhuān)為移動(dòng)端設(shè)計(jì)的輕量級(jí)組件庫(kù),自2017年開(kāi)源以來(lái),提供了Vue2、Vue3及多平臺(tái)版本支持,安裝Vant時(shí)需要注意版本兼容問(wèn)題,Vue3項(xiàng)目應(yīng)安裝最新版Vant3,而Vue2項(xiàng)目則需安裝Vant2,安裝錯(cuò)誤時(shí),需卸載后重新安裝正確版本
    2024-10-10
  • Vite3遷移Webpack5的實(shí)現(xiàn)

    Vite3遷移Webpack5的實(shí)現(xiàn)

    本文主要介紹了Vite3遷移Webpack5的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2023-04-04
  • vue實(shí)現(xiàn)動(dòng)態(tài)按鈕功能

    vue實(shí)現(xiàn)動(dòng)態(tài)按鈕功能

    這篇文章主要介紹了vue實(shí)現(xiàn)動(dòng)態(tài)按鈕功能,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2019-05-05
  • Vue使用Echarts實(shí)現(xiàn)數(shù)據(jù)可視化的方法詳解

    Vue使用Echarts實(shí)現(xiàn)數(shù)據(jù)可視化的方法詳解

    這篇文章主要為大家詳細(xì)介紹了Vue使用Echarts實(shí)現(xiàn)數(shù)據(jù)可視化的方法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來(lái)幫助
    2022-03-03
  • vue3中reactive和ref函數(shù)及對(duì)比分析

    vue3中reactive和ref函數(shù)及對(duì)比分析

    這篇文章主要介紹了vue3中reactive和ref函數(shù)及對(duì)比,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2024-01-01
  • vue登錄注冊(cè)實(shí)例詳解

    vue登錄注冊(cè)實(shí)例詳解

    在本篇內(nèi)容里小編給大家分享的是關(guān)于vue登錄注冊(cè)的相關(guān)實(shí)例內(nèi)容以及寫(xiě)法分析,有需要朋友們可以學(xué)習(xí)下。
    2019-09-09
  • 在vue中使用Echarts利用watch做動(dòng)態(tài)數(shù)據(jù)渲染操作

    在vue中使用Echarts利用watch做動(dòng)態(tài)數(shù)據(jù)渲染操作

    這篇文章主要介紹了在vue中使用Echarts利用watch做動(dòng)態(tài)數(shù)據(jù)渲染操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2020-07-07
  • vue-element如何實(shí)現(xiàn)動(dòng)態(tài)換膚存儲(chǔ)

    vue-element如何實(shí)現(xiàn)動(dòng)態(tài)換膚存儲(chǔ)

    這篇文章主要介紹了vue-element如何實(shí)現(xiàn)動(dòng)態(tài)換膚存儲(chǔ)問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-04-04
  • 關(guān)于引入vue.js 文件的知識(shí)點(diǎn)總結(jié)

    關(guān)于引入vue.js 文件的知識(shí)點(diǎn)總結(jié)

    在本篇文章里小編給大家分享的是關(guān)于引入vue.js 文件的知識(shí)點(diǎn)總結(jié),有需要的朋友們可以參考學(xué)習(xí)下。
    2020-01-01
  • 詳解Vue返回值動(dòng)態(tài)生成表單及提交數(shù)據(jù)的辦法

    詳解Vue返回值動(dòng)態(tài)生成表單及提交數(shù)據(jù)的辦法

    這篇文章主要為大家介紹了Vue返回值動(dòng)態(tài)生成表單及提交數(shù)據(jù),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來(lái)幫助
    2021-12-12

最新評(píng)論

鄂托克前旗| 琼中| 板桥市| 交口县| 平顶山市| 河西区| 上林县| 屯门区| 海林市| 阿拉善左旗| 肥乡县| 太谷县| 东山县| 博爱县| 荆州市| 阳朔县| 常德市| 行唐县| 多伦县| 墨江| 土默特左旗| 滁州市| 固始县| 绥芬河市| 肥城市| 襄汾县| 永登县| 花垣县| 嘉定区| 乌什县| 垫江县| 平果县| 棋牌| 喜德县| 梨树县| 自治县| 万州区| 和田县| 南通市| 玉屏| 通化市|