亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Vue3通過ref操作Dom元素及hooks的使用方法

 更新時(shí)間:2023年01月26日 12:41:23   作者:Nanchen_42  
這篇文章主要介紹了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)文章

  • Vue分頁查詢?cè)趺磳?shí)現(xiàn)

    Vue分頁查詢?cè)趺磳?shí)現(xiàn)

    這篇文章主要介紹了Vue分頁查詢?cè)趺磳?shí)現(xiàn),使用vue實(shí)現(xiàn)分頁的邏輯并不復(fù)雜,接收后端傳輸過來的數(shù)據(jù),然后根據(jù)數(shù)據(jù)的總數(shù)和每一頁的數(shù)據(jù)量就可以計(jì)算出一共可以分成幾頁
    2023-04-04
  • Vue之計(jì)算屬性詳解

    Vue之計(jì)算屬性詳解

    這篇文章主要為大家介紹了Vue的計(jì)算屬性,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助
    2021-11-11
  • vue3+vite assets動(dòng)態(tài)引入圖片的三種方法及解決打包后圖片路徑錯(cuò)誤不顯示的問題

    vue3+vite assets動(dòng)態(tài)引入圖片的三種方法及解決打包后圖片路徑錯(cuò)誤不顯示的問題

    這篇文章主要介紹了vue3+vite assets動(dòng)態(tài)引入圖片的幾種方式,解決打包后圖片路徑錯(cuò)誤不顯示的問題,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2023-03-03
  • vue 解決provide和inject響應(yīng)的問題

    vue 解決provide和inject響應(yīng)的問題

    這篇文章主要介紹了vue 解決provide和inject響應(yīng)的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2020-11-11
  • Vue-less的使用和deep深度選擇器詳解

    Vue-less的使用和deep深度選擇器詳解

    這篇文章主要介紹了Vue-less的使用和deep深度選擇器,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-10-10
  • Vue2?與?Vue3?的數(shù)據(jù)綁定原理及實(shí)現(xiàn)

    Vue2?與?Vue3?的數(shù)據(jù)綁定原理及實(shí)現(xiàn)

    這篇文章主要介紹了Vue2與Vue3的數(shù)據(jù)綁定原理及實(shí)現(xiàn),數(shù)據(jù)綁定是一種把用戶界面元素的屬性綁定到特定對(duì)象上面并使其同步的機(jī)制,使開發(fā)人員免于編寫同步視圖模型和視圖的邏輯
    2022-09-09
  • 詳解Vue中$refs和$nextTick的使用方法

    詳解Vue中$refs和$nextTick的使用方法

    這篇文章主要為大家介紹了Vue中$refs和$nextTick的使用方法,文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)Vue有一定幫助,需要的可以參考一下
    2022-03-03
  • vue組件中節(jié)流函數(shù)的失效的原因和解決方法

    vue組件中節(jié)流函數(shù)的失效的原因和解決方法

    這篇文章主要介紹了vue組件中節(jié)流函數(shù)的失效和解決方法,幫助大家更好的理解和學(xué)習(xí)vue框架,感興趣的朋友可以了解下
    2020-12-12
  • ElementUI?el-table?樹形數(shù)據(jù)的懶加載的實(shí)現(xiàn)

    ElementUI?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-06
  • Vue3新特性Suspense和Teleport應(yīng)用場(chǎng)景分析

    Vue3新特性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

最新評(píng)論