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

Vue中四種操作dom方法保姆級講解

 更新時間:2023年02月01日 09:37:07   作者:山山而川~xyj  
這篇文章主要介紹了Vue中四種操作dom方法,首先,在vue中強(qiáng)烈禁用原生與jquery來操作DOM元素。我們要充分的利用vue的優(yōu)勢:MVVM,在vue中程序員幾乎不操作DOM,只需要維護(hù)好數(shù)據(jù)即可,vue給程序員提供ref引用,不調(diào)用api直接獲取元素組件的使用

前言

最近主管提出了許多優(yōu)化用戶體驗(yàn)的要求,其中很多涉及 dom 操作。本文將 Vue3 中常見的 dom 操作總結(jié)了一下。

一、通過ref拿到dom的引用

<template>
    <div class="ref-container">
        <div ref="sectionRef" class="ref-section"></div>
    </div>
</template>
<script lang="ts" setup>
import { ref } from 'vue'
const sectionRef = ref()
</script>

通過對 div 元素添加 ref 屬性,為了獲取到這個元素,我們聲明了一個與 ref 屬性名稱相同的變量,然后通過 [變量名].value 的形式即可獲取該 div 元素。

適用場景

單一 dom 元素或者個數(shù)較少的場景

示例代碼

<template>
    <div class="ref-container">
        <p>通過 ref 直接拿到 dom</p>
        <div ref="sectionRef" class="ref-section"></div>
        <button @click="action" class="btn">變高</button>
    </div>
</template>
<script lang="ts" setup>
import { ref } from 'vue'
const sectionRef = ref()
let height = 100;
const action= () => {
    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引用

通過對父元素添加 ref 屬性,并聲明一個與 ref 屬性名稱相同的變量 list,此時通過 list.value 會獲得包含子元素的 dom 對象。此時可以通過 list.value.children[index] 的形式獲取子元素 dom

<template>
    <div class="ref-container">
        <div ref="list" 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 lang="ts" setup>
import { ref, reactive } from 'vue'
const list = ref()

適用場景

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

示例代碼

<template>
    <div class="ref-container">
        <p>通過父容器遍歷拿到dom</p>
        <div ref="list" 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 lang="ts" setup>
import { ref, reactive } from 'vue'
const list = 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>

三、通過子組件emit傳遞ref

通過對子組件添加 ref 屬性,并聲明一個與 ref 屬性名稱相同的變量 childRef,此時通過 emitchildRef.value 作為一個 dom 引用傳遞出去。

<template>
    <div ref="childRef" @click="cellAction" class="cell-item">
        <span>{{item}}</span>
    </div>
</template>
<script lang="ts" setup>
import { ref } from 'vue'
const props = defineProps({
    item: Number
})
const emit = defineEmits(['cellTap']);
const childRef = ref();
const cellAction = () => {
    emit('cellTap', childRef.value);
}
</script>

適用場景

多個頁面都可能有操作組件 dom 的場景

示例代碼

<template>
    <div ref="childRef" @click="cellAction" class="cell-item">
        <span>{{item}}</span>
    </div>
</template>
<script lang="ts" setup>
import { ref } from 'vue'
const props = defineProps({
    item: Number
})
const emit = defineEmits(['cellTap']);
const childRef = ref()
const cellAction = () => {
    emit('cellTap', childRef.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="ref-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 lang="ts" setup>
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>

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

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

<template>
    <div class="ref-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 lang="ts" setup>
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>

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

適用場景

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

示例代碼

<template>
    <div class="ref-container">
        <p>通過: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 lang="ts" setup>
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ān)于Vue中四種操作dom方法保姆級講解的文章就介紹到這了,更多相關(guān)Vue操作dom內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • vue動態(tài)設(shè)置頁面title的方法實(shí)例

    vue動態(tài)設(shè)置頁面title的方法實(shí)例

    這篇文章主要給大家介紹了關(guān)于vue動態(tài)設(shè)置頁面title的相關(guān)資料,文中通過實(shí)例代碼結(jié)束的非常詳細(xì),對大家學(xué)習(xí)或者使用Vue具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-08-08
  • 解決vue前后端端口不一致的問題

    解決vue前后端端口不一致的問題

    這篇文章主要介紹了解決vue前后端端口不一致的問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-07-07
  • Vue簡單實(shí)現(xiàn)原理詳解

    Vue簡單實(shí)現(xiàn)原理詳解

    這篇文章主要介紹了Vue簡單實(shí)現(xiàn)原理,結(jié)合實(shí)例形式詳細(xì)分析了Vue實(shí)現(xiàn)原理與操作注意事項(xiàng),需要的朋友可以參考下
    2020-05-05
  • vue實(shí)現(xiàn)簡潔文件上傳進(jìn)度條功能

    vue實(shí)現(xiàn)簡潔文件上傳進(jìn)度條功能

    這篇文章主要介紹了vue實(shí)現(xiàn)簡潔文件上傳進(jìn)度條功能,實(shí)現(xiàn)原理是通過performance.now()獲取動畫的時間戳,用于創(chuàng)建流暢的動畫,結(jié)合示例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2024-06-06
  • vue項(xiàng)目使用axios封裝request請求的過程

    vue項(xiàng)目使用axios封裝request請求的過程

    這篇文章主要介紹了vue項(xiàng)目使用axios封裝request請求,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2023-04-04
  • vue實(shí)現(xiàn)書籍購物車功能

    vue實(shí)現(xiàn)書籍購物車功能

    這篇文章主要為大家詳細(xì)介紹了vue實(shí)現(xiàn)書籍購物車功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-10-10
  • vue簡單練習(xí) 桌面時鐘的實(shí)現(xiàn)代碼實(shí)例

    vue簡單練習(xí) 桌面時鐘的實(shí)現(xiàn)代碼實(shí)例

    這篇文章主要介紹了vue簡單練習(xí) 桌面時鐘的實(shí)現(xiàn)代碼實(shí)例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值的相關(guān)資料,需要的朋友可以參考下
    2019-09-09
  • 解決vite build打包后頁面不能正常訪問的情況

    解決vite build打包后頁面不能正常訪問的情況

    這篇文章主要介紹了解決Vite打包后直接使用瀏覽器打開,顯示空白問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-03-03
  • vue.js element-ui tree樹形控件改iview的方法

    vue.js element-ui tree樹形控件改iview的方法

    這篇文章主要介紹了vue.js element-ui tree樹形控件改iview的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-03-03
  • vue router導(dǎo)航守衛(wèi)(router.beforeEach())的使用詳解

    vue router導(dǎo)航守衛(wèi)(router.beforeEach())的使用詳解

    導(dǎo)航守衛(wèi)主要用來通過跳轉(zhuǎn)或取消的方式守衛(wèi)導(dǎo)航。這篇文章主要介紹了vue-router導(dǎo)航守衛(wèi)(router.beforeEach())的使用,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價值,需要的朋友可以參考下
    2019-04-04

最新評論

侯马市| 新和县| 兴隆县| 武安市| 禹州市| 三穗县| 博客| 黄梅县| 绥滨县| 乐平市| 稻城县| 石嘴山市| 含山县| 武夷山市| 松滋市| 宁陵县| 确山县| 九寨沟县| 特克斯县| 桦南县| 漠河县| 肃北| 和平区| 紫阳县| 宜君县| 石门县| 偃师市| 贡嘎县| 饶平县| 乐至县| 沾益县| 鄄城县| 开江县| 句容市| 博爱县| 新巴尔虎右旗| 涞水县| 比如县| 宾川县| 马鞍山市| 容城县|