08_Vue3 自定义hook函数

目录

自定义hook函数

例如:

[src下新建文件夹 hooks](#src下新建文件夹 hooks)

[创建js文件,文件名为 usePoint.js](#创建js文件,文件名为 usePoint.js)

[在 App.vue 中](#在 App.vue 中)

[Demo.vue 组件中](#Demo.vue 组件中)

[Test.vue 组件中](#Test.vue 组件中)


自定义hook函数

  • 什么是 hook ?------本质是一个函数,把 setup 函数中使用的 Composition API 进行了封装。
  • 类似于 vue2 中的 mixin。
  • 自定义hook的优势:复用代码,让 setup 中的逻辑更清楚易懂。

例如:

src下新建文件夹 hooks

创建js文件,文件名为 usePoint.js

在 usePoint.js 中封装一个鼠标点击"打点"的函数 savePoint

javascript 复制代码
import {reactive,onMounted,onBeforeUnmount} from 'vue'
function savePoint(){
    //实现鼠标'打点'相关的数据
    let point = reactive({
        x:0,
        y:0
    })

    //实现鼠标'打点'相关的方法
    function savePoint(event){
        point.x = event.pageX
        point.y = event.pageY
        console.log(event.pageX,event.pageY)
    }
    //实现鼠标'打点'相关的生命周期钩子

    onMounted(()=>{
        window.addEventListener("click", savePoint)
    })

    onBeforeUnmount(()=>{
        window.removeEventListener("click", savePoint)
    })

    //一定要返回,否则无法使用point
    return point
}

export default savePoint

在 App.vue 中

用到了两个组件,分别是Demo.vue 和 Test.vue

javascript 复制代码
<template>
  <button @click="xianshi = !xianshi">点我移除Demo</button>
  <Demo v-if="xianshi"></Demo>
  <hr>
  <Test></Test>
</template>

<script>
  import {ref} from 'vue'
  import Demo from './components/Demo.vue'
  import Test from './components/Test'
  export default {
    name: 'App',
    components:{Demo,Test},
    setup(){
      let xianshi = ref(true)

      return {
        xianshi
      }
    }
  }
</script>

Demo.vue 组件中

引入封装好的 usePoint.js ,可以直接使用返回的 point

javascript 复制代码
<template>
  <h2>当前求和为:{{sum}}</h2>
  <button @click="sum++">点我+1</button>
  <hr>
  <h2>当前点击时鼠标的坐标为:x:{{point.x}},y:{{point.y}}</h2>

</template>

<script>
import {ref,reactive,onMounted,onBeforeUnmount} from 'vue'
import usePoint from '../hooks/usePoint'
export default {
  name: 'Demo',
  setup() {
    //数据
    let sum = ref(0)

    let point = usePoint()
    //返回一个对象(常用)
    return {
      sum,
      point
    }
  }
}
</script>

Test.vue 组件中

引入封装好的 usePoint.js ,可以直接使用返回的 point

javascript 复制代码
<template>
  <h2>我是Test组件</h2>
  <h3>姓名:张三</h3>
  <h3>鼠标点击的坐标:x:{{point.x}},y:{{point.y}}</h3>
</template>

<script>
import {ref} from 'vue'
import usePoint from '../hooks/usePoint'
export default {
  name: "test",
  setup(){
    let name = ref('张三')
    let point = usePoint()

    return{
      name,
      point
    }

  }
}
</script>

<style scoped>

</style>
相关推荐
走在路上的菜鸟4 小时前
Android学Dart学习笔记第十节 循环
android·笔记·学习·flutter
AAA阿giao4 小时前
JavaScript 执行机制深度解析:从 V8 引擎到作用域链、变量提升与闭包的全面剖析
前端·javascript·面试
一水鉴天4 小时前
整体设计 定稿 之19 拼语言表述体系之2(codebuddy)
大数据·前端·人工智能·架构
低代码的未来4 小时前
React CVE-2025-55182漏洞排查与修复指南
前端
脾气有点小暴5 小时前
CSS position 属性
前端·css
询问QQ:276998855 小时前
基于Skyhook和地棚控制的半主动悬架模型——详细解读1/4车辆平顺性评价指标及MATLAB...
vue.js
ohyeah5 小时前
用原生 JS 手写一个“就地编辑”组件:EditInPlace 的 OOP 实践
前端·javascript
Xudde.5 小时前
friendly靶机渗透
笔记·学习·安全·web安全·php
timeweaver5 小时前
React Server Components 的致命漏洞CVE-2025-55182
前端·安全
重铸码农荣光5 小时前
深入理解 JavaScript 中的 this:一场关于作用域、调用方式与设计哲学的思辨
前端·javascript