【vue3】自定义hook函数

假期第三篇,对于基础的知识点,我感觉自己还是很薄弱的。
趁着假期,再去复习一遍

【vue3 】hook函数

hook本质上是一个函数,把setup中使用的Composition API进行了封装

假设需求是获取当前点击时鼠标的坐标

<template>
  <div>
    <h2>当前求和的值为:{{ sum }}</h2>
    <button @click="sum++">点我+1</button>
    <hr />
    <h2>当前点击时鼠标的坐标为:x:{{ point.x }},y:{{ point.y }}</h2>
  </div>
</template>
<script >
import { ref, reactive, onMounted, onBeforeMount } from "vue";
export default {
  name: "demo",
  setup() {
    let sum = ref(0);
    let point = reactive({
      x: 0,
      y: 0,
    });
    onMounted(() => {
      window.addEventListener("click", savePoint);
    });
    onBeforeMount(() => {
      window.removeEventListener("click", savePoint);
    });
    function savePoint(event) {
      (point.x = event.pageX), (point.y = event.pageY);
    }
    return {
      sum,
      point,
    };
  },
};
</script>

在这里插入图片描述
假设还有其他页面也需要用到点击鼠标求坐标的需求,那就可以把这些代码都放到hook函数中,实现代码的复用
在这里插入图片描述
src下新建hoos文件夹,新建usePoint.js,有两种写法

写法1:

import {  reactive, onMounted, onBeforeMount } from "vue";

function savePoint() {
     let point = reactive({
      x: 0,
      y: 0,
    });
    onMounted(() => {
      window.addEventListener("click", savePoint);
    });
    onBeforeMount(() => {
      window.removeEventListener("click", savePoint);
    });
    function savePoint(event) {
      (point.x = event.pageX), (point.y = event.pageY);
    }
    return {
        point
    }
}
 export default savePoint

在页面中使用

<template>
  <div>
    <h2>当前求和的值为:{{ sum }}</h2>
    <button @click="sum++">点我+1</button>
    <hr />
    <h2>当前点击时鼠标的坐标为:x:{{ point.x }},y:{{ point.y }}</h2>
  </div>
</template>
<script >
import { ref} from "vue";
import savePoint from "./hooks/usePoint";
export default {
  name: "demo",
  setup() {
    let sum = ref(0); 
    const { point } = savePoint();
    watch(point, (newVal, oldVal) => {
      console.log("point变了", newVal, oldVal);
    });
    return {
      sum,
      point,
    };
  },
};
</script>

在这里插入图片描述
写法2:

import {  reactive, onMounted, onBeforeMount } from "vue";
 export default function () {
     let point = reactive({
      x: 0,
      y: 0,
    });
    onMounted(() => {
      window.addEventListener("click", savePoint);
    });
    onBeforeMount(() => {
      window.removeEventListener("click", savePoint);
    });
    function savePoint(event) {
      (point.x = event.pageX), (point.y = event.pageY);
     }
     return point
}

在页面中使用

<template>
  <div>
    <h2>当前求和的值为:{{ sum }}</h2>
    <button @click="sum++">点我+1</button>
    <hr />
    <h2>当前点击时鼠标的坐标为:x:{{ point.x }},y:{{ point.y }}</h2>
  </div>
</template>
<script >
import { ref } from "vue";
import usePoint from "./hooks/usePoint";
export default {
  name: "demo",
  setup() {
    let sum = ref(0);
    let point = usePoint();
    watch(point, (newVal, oldVal) => {
      console.log("point变了", newVal, oldVal);
    });
    return {
      sum,
      point,
    };
  },
};
</script>

在这里插入图片描述

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值