Vue3中操作ref的四種使用方式示例代碼(建議收藏)
前言
最近產(chǎn)品經(jīng)理提出了很多用戶體驗(yàn)優(yōu)化的需求,涉及到很多dom的操作。
小張:“老鐵,本來開發(fā)Vue2項(xiàng)目操作dom挺簡單的,現(xiàn)在開發(fā)vue3項(xiàng)目,突然感覺一頭霧水!”
我:“沒事,原理都差不多,查查資料應(yīng)該沒問題的!”
至此將Vue3中dom操作常見的幾種方式總結(jié)一下!
通過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元素或者個數(shù)較少的場景
示例代碼
<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)生成的固定數(shù)量元素的場景
示例代碼
<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引用放到數(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>
通過:ref循環(huán)調(diào)用setRefAction方法,該方法會默認(rèn)接收一個el參數(shù),這個參數(shù)就是我們需要獲取的div元素
此時可以通過state.refList[index]的形式獲取子元素dom
適用場景
通過v-for循環(huán)生成的不固定數(shù)量或者多種元素的場景
示例代碼
<template> <div class="demo2-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 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>
寫在最后
推薦幾個作者參與的開源項(xiàng)目,如果項(xiàng)目有幫助到你,歡迎star!
一個簡單的基于Vue3、TS、Vite、qiankun技術(shù)棧的后臺管理項(xiàng)目
:www.xkxk.tech
一個基于Vue3、Vite的仿element UI的組件庫項(xiàng)目
:ui.xkxk.tech
一個基于Vue3、Vite的炫酷大屏項(xiàng)目
:screen.xkxk.tech
到此這篇關(guān)于Vue3中操作ref的四種使用方式示例代碼(建議收藏)的文章就介紹到這了,更多相關(guān)Vue3操作ref內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue實(shí)現(xiàn)短信驗(yàn)證碼登錄功能(流程詳解)
無論是移動端還是pc端登錄或者注冊界面都會見到手機(jī)驗(yàn)證碼登錄這個功能,輸入手機(jī)號,得到驗(yàn)證碼,這篇文章主要介紹了基于vue實(shí)現(xiàn)短信驗(yàn)證碼登錄功能,需要的朋友可以參考下2019-12-12vue實(shí)現(xiàn)全屏滾動效果(非fullpage.js)
這篇文章主要為大家詳細(xì)介紹了vue實(shí)現(xiàn)全屏滾動效果,非fullpage.js,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2020-03-03基于vue實(shí)現(xiàn)循環(huán)滾動列表功能
這篇文章給大家介紹基于vue實(shí)現(xiàn)循環(huán)滾動列表功能,給大家介紹了該組件的用法,通過實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友參考下吧2021-09-09Vue項(xiàng)目打包優(yōu)化實(shí)踐指南(推薦!)
如果引入的庫眾多,那么vendor.js文件體積將會相當(dāng)?shù)拇?影響首開的體驗(yàn),這篇文章主要給大家介紹了關(guān)于Vue項(xiàng)目打包優(yōu)化實(shí)踐的相關(guān)資料,需要的朋友可以參考下2022-06-06Vue.js中動態(tài)更改svg的相關(guān)屬性詳解
這篇文章主要為大家介紹了Vue.js中動態(tài)更改svg的相關(guān)屬性詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-02-02Vue動態(tài)修改網(wǎng)頁標(biāo)題的方法及遇到問題
Vue下有很多的方式去修改網(wǎng)頁標(biāo)題,這里總結(jié)下解決此問題的幾種方案:,需要的朋友可以參考下2019-06-06Vue單頁式應(yīng)用(Hash模式下)實(shí)現(xiàn)微信分享的實(shí)例
本篇文章介紹了Vue單頁式應(yīng)用(Hash模式下)實(shí)現(xiàn)微信分享的實(shí)例,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-07-07