Vue3通過ref操作Dom元素及hooks的使用方法
Vue3 ref獲取DOM元素
<div ref="divBox">Hello</div>
import {ref,onMounted} from 'vue'
setup() { const divBox = ref(null); onMounted(()=>{ console.log(divBox.value); }) return{divBox} }
父組件監(jiān)聽子組件中的元素
在父組件中的子組件里會(huì)打印一個(gè)proxy(實(shí)例),通過實(shí)例去獲取里面的屬性或者值
setup() { const commer = ref(null) onMounted(()=>{ console.log(commer); console.log(commer.value); }) return { commer } }
看這個(gè)例子:
父組件:
<template> <div class="about"> <h1>This is an about page</h1> <com ref="commer"></com> <h3>通過ref用父組件接收子組件中的寬和高:<span>{{numWidht}} {{numHeight}}</span></h3> </div> </template> <script> import com from '../components/com.vue' import {ref,onMounted} from 'vue' export default { components: { com }, setup() { const commer = ref(null) const numWidht = ref(0); const numHeight = ref(0) onMounted(()=>{ numWidht.value =commer.value.width numHeight.value =commer.value.height }) return { commer,numWidht,numHeight } } } </script>
子組件:
<template> <h1>屏幕尺寸:</h1> <h1>寬度:{{width}}</h1> <h1>高度:{{height}}</h1> </template> <script> // import { ref,onMounted } from 'vue'; import useWindwoResize from '../hooks/useWindowResize' export default { setup(){ const {width, height} = useWindwoResize() return{width,height} } }; </script> <style lang="scss" scoped> </style>
hooks頁面:
import {onMounted, onUnmounted, ref} from 'vue'; function useWindowResize(){ const width = ref(0) const height = ref(0) function onResize(){ width.value = window.innerWidth height.value = window.innerHeight } onMounted(()=>{ window.addEventListener("resize",onResize); onResize(); }) onUnmounted(()=>{ window.removeEventListener('resize',onResize); }) return{ width, height } } export default useWindowResize;
Vue3 hooks
在vue3中的hooks其實(shí)就是函數(shù)的寫法,就是將文件的一些單獨(dú)功能的js代碼進(jìn)行抽離出來,放到單獨(dú)的js文件中。這樣其實(shí)和我們?cè)趘ue2中學(xué)的混入(mixin)比較像。
父組件
<h1>屏幕尺寸:</h1> <div>寬度:{{ width }}</div> <div>高度:{{ height }}</div>
引入hooks中的js文件
import useWindwoResize from '../hooks/useWindowResize'; setup(){ const {width, height} = useWindwoResize() return{width,height} }
新建hooks文件夾在里面新建useWindowResize.js文件,內(nèi)容如下:
import {onMounted, onUnmounted, ref} from 'vue'; function useWindowResize(){ const width = ref(0) const height = ref(0) function onResize(){ width.value = window.innerWidth height.value = window.innerHeight } onMounted(()=>{ window.addEventListener("resize",onResize); onResize(); }) onUnmounted(()=>{ window.removeEventListener('resize',onResize); }) return{ width, height } } export default useWindowResize;
到此這篇關(guān)于Vue3通過ref操作Dom元素及hooks的使用方法的文章就介紹到這了,更多相關(guān)Vue3通過ref操作Dom元素及hooks的使用方法內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue3+vite assets動(dòng)態(tài)引入圖片的三種方法及解決打包后圖片路徑錯(cuò)誤不顯示的問題
這篇文章主要介紹了vue3+vite assets動(dòng)態(tài)引入圖片的幾種方式,解決打包后圖片路徑錯(cuò)誤不顯示的問題,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-03-03vue 解決provide和inject響應(yīng)的問題
這篇文章主要介紹了vue 解決provide和inject響應(yīng)的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-11-11Vue2?與?Vue3?的數(shù)據(jù)綁定原理及實(shí)現(xiàn)
這篇文章主要介紹了Vue2與Vue3的數(shù)據(jù)綁定原理及實(shí)現(xiàn),數(shù)據(jù)綁定是一種把用戶界面元素的屬性綁定到特定對(duì)象上面并使其同步的機(jī)制,使開發(fā)人員免于編寫同步視圖模型和視圖的邏輯2022-09-09vue組件中節(jié)流函數(shù)的失效的原因和解決方法
這篇文章主要介紹了vue組件中節(jié)流函數(shù)的失效和解決方法,幫助大家更好的理解和學(xué)習(xí)vue框架,感興趣的朋友可以了解下2020-12-12ElementUI?el-table?樹形數(shù)據(jù)的懶加載的實(shí)現(xiàn)
當(dāng)面對(duì)大量數(shù)據(jù)時(shí),一次性加載所有數(shù)據(jù)可能會(huì)導(dǎo)致性能問題,我們可以實(shí)現(xiàn)樹形數(shù)據(jù)的懶加載,本文主要介紹了ElementUI?el-table?樹形數(shù)據(jù)的懶加載,感興趣的可以了解一下2024-06-06Vue3新特性Suspense和Teleport應(yīng)用場(chǎng)景分析
本文介紹了Vue2和Vue3中的Suspense用于處理異步請(qǐng)求的加載提示,以及如何在組件間實(shí)現(xiàn)動(dòng)態(tài)加載,同時(shí),Teleport技術(shù)展示了如何在DOM中靈活地控制組件的渲染位置,解決布局問題,感興趣的朋友跟隨小編一起看看吧2024-07-07