Vue3通過(guò)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)聽(tīng)子組件中的元素
在父組件中的子組件里會(huì)打印一個(gè)proxy(實(shí)例),通過(guò)實(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>通過(guò)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頁(yè)面:
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ù)的寫(xiě)法,就是將文件的一些單獨(dú)功能的js代碼進(jìn)行抽離出來(lái),放到單獨(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通過(guò)ref操作Dom元素及hooks的使用方法的文章就介紹到這了,更多相關(guān)Vue3通過(guò)ref操作Dom元素及hooks的使用方法內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- vue3封裝Hooks實(shí)現(xiàn)圖片懶加載指南
- Vue3官方Hooks的用法與實(shí)現(xiàn)原理
- Vue3使用hooks解決字典數(shù)據(jù)的顯示問(wèn)題
- Vue3通過(guò)hooks方式封裝節(jié)流和防抖的代碼詳解
- Vue3封裝hooks實(shí)現(xiàn)實(shí)時(shí)獲取麥克風(fēng)音量
- Vue3中Hooks封裝的技巧詳解
- Vue3自定義Hooks函數(shù)的使用詳解
- 詳解hooks在vue3中的使用方法及示例
- vue3的hooks用法總結(jié)
- vue3中hooks的概述及用法小結(jié)
- Vue3 Hooks使用實(shí)戰(zhàn)
相關(guān)文章
Vue項(xiàng)目結(jié)合Vue-layer實(shí)現(xiàn)彈框式編輯功能(實(shí)例代碼)
這篇文章主要介紹了Vue項(xiàng)目中結(jié)合Vue-layer實(shí)現(xiàn)彈框式編輯功能,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-03-03
unplugin-auto-import的配置以及eslint報(bào)錯(cuò)解決詳解
unplugin-auto-import?解決了vue3-hook、vue-router、useVue等多個(gè)插件的自動(dòng)導(dǎo)入,也支持自定義插件的自動(dòng)導(dǎo)入,是一個(gè)功能強(qiáng)大的typescript支持工具,這篇文章主要給大家介紹了關(guān)于unplugin-auto-import的配置以及eslint報(bào)錯(cuò)解決的相關(guān)資料,需要的朋友可以參考下2022-08-08
一文詳解vue-router中的導(dǎo)航守衛(wèi)
vue-router提供的導(dǎo)航守衛(wèi)主要用來(lái)通過(guò)跳轉(zhuǎn)或取消的方式守衛(wèi)導(dǎo)航,在 vue-router 中,導(dǎo)航守衛(wèi)是一種非常重要的功能,所以本文將詳細(xì)講解一下vue-router中的導(dǎo)航守衛(wèi),感興趣的同學(xué)跟著小編一起來(lái)看看吧2023-07-07
electron-vite工具打包后如何通過(guò)內(nèi)置配置文件動(dòng)態(tài)修改接口地址
使用electron-vite?工具開(kāi)發(fā)項(xiàng)目打包完后每次要改接口地址都要重新打包,對(duì)于多環(huán)境切換或者頻繁變更接口地址就顯得麻煩,這篇文章主要介紹了electron-vite工具打包后通過(guò)內(nèi)置配置文件動(dòng)態(tài)修改接口地址實(shí)現(xiàn)方法,需要的朋友可以參考下2024-05-05
vue2 拖動(dòng)排序 vuedraggable組件的實(shí)現(xiàn)
這篇文章主要介紹了vue2 拖動(dòng)排序 vuedraggable組件的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-08-08

