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)聽子組件中的元素
在父組件中的子組件里會(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ù)的寫法,就是將文件的一些單獨(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)文章希望大家以后多多支持腳本之家!
相關(guān)文章
element-ui 關(guān)于獲取select 的label值方法
今天小編就為大家分享一篇element-ui 關(guān)于獲取select 的label值方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-08-08
基于Vue 2.0 監(jiān)聽文本框內(nèi)容變化及ref的使用說(shuō)明介紹
今天小編就為大家分享一篇基于Vue 2.0 監(jiān)聽文本框內(nèi)容變化及ref的使用說(shuō)明介紹,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-08-08
vue3.2自定義彈窗組件結(jié)合函數(shù)式調(diào)用示例詳解
這篇文章主要為大家介紹了vue3.2自定義彈窗組件結(jié)合函數(shù)式調(diào)用示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-09-09
setup+ref+reactive實(shí)現(xiàn)vue3響應(yīng)式功能
這篇文章介紹了通過(guò)setup+ref+reactive實(shí)現(xiàn)vue3響應(yīng)式功能,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-11-11
vue實(shí)現(xiàn)在v-html的html字符串中綁定事件
今天小編就為大家分享一篇vue實(shí)現(xiàn)在v-html的html字符串中綁定事件,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-10-10

