当前位置:   article > 正文

Vue3 Hooks函数使用及封装思想_vue3 hooks封装

vue3 hooks封装

目录

一. 什么是hooks函数?

二、如何封装一个hooks函数

三、Hooks 常用 Demo

(1)验证码倒计时

(2)防抖

(3)节流


一. 什么是hooks函数?

专业解释:Vue 3中的Hooks函数是一种用于在组件中共享可复用逻辑的方式。

大白话:将单独功能的js代码抽离出来, 加工成公共函数,从而达到逻辑复用。

在尤大大开发Vue3  Composition API 主要考虑了以下两点 :

  1. 对Vue社区调研,了解了许多使用Vue的开发者对于更好的组件逻辑组织方式的期望。
  2. 对React Hooks和其他前端框架的解决方案进行了学习和借鉴。

有了composition API 意味着我们就可以自定义封装hooks,最终的目的都是进行复用,在Vue2中复用的方式大部分都是采取的mixin但相比hooks,hooks更清楚复用的功能来源及功能

二、如何封装一个hooks函数

例如实现一个点击按钮获取body的宽度和高度

  1. <script setup lang="ts">
  2. import { reactive } from "vue";
  3. const data = reactive({
  4. width: 0,
  5. height:0
  6. })
  7. const getViewportSize = () => {
  8. data.width = document.body.clientWidth;
  9. data.height = document.body.clientHeight;
  10. }
  11. </script>
  12. <template>
  13. <button @click="getViewportSize" > 获取窗口大小 </button>
  14. <div>
  15. <div>宽度 : {{data.width}}</div>
  16. <div>宽度 : {{data.height}}</div>
  17. </div>
  18. </template>

抽离逻辑,封装成hooks函数

hooks封装规范:

1. 新建hooks文件;

2. 函数名前缀加上use;

3. 合理利用Vue提供的响应式函数及生命周期;

4. 暴露出 变量 和 方法 提供外部需要时使用;

src/hooks/index.js

  1. import { reactive } from "vue";
  2. export function useVwSize() {
  3. const data = reactive({
  4. width: 0,
  5. height:0
  6. })
  7. const getViewportSize = () => {
  8. data.width = document.body.clientWidth;
  9. data.height = document.body.clientHeight;
  10. }
  11. return {
  12. data,getViewportSize
  13. }
  14. }

index.vue 使用hooks

  1. <script setup lang="ts">
  2. import { useVwSize } from "@/hooks";
  3. const { data, getViewportSize } = useVwSize();
  4. </script>
  5. <template>
  6. <button @click="getViewportSize">获取窗口大小</button>
  7. <div>
  8. <div>宽度 : {{ data.width }}</div>
  9. <div>宽度 : {{ data.height }}</div>
  10. </div>
  11. </template>

三、Hooks 常用 Demo

(1)验证码倒计时

  1. /**
  2. * 倒计时
  3. * @param {Number} second 倒计时秒数
  4. * @return {Number} count 倒计时秒数
  5. * @return {Function} countDown 倒计时函数
  6. * @example
  7. * const { count, countDown } = useCountDown()
  8. * countDown(60)
  9. * <div>{{ count }}</div>
  10. */
  11. export function useCountDown() {
  12. const count = ref(0);
  13. const timer = ref(null);
  14. const countDown = (second= 60 , ck = () => {}) => {
  15. if (count.value === 0 && timer.value === null) {
  16. ck();
  17. count.value = second;
  18. timer.value = setInterval(() => {
  19. count.value--;
  20. if (count.value === 0) {
  21. clearInterval(timer.value);
  22. timer.value = null
  23. }
  24. }, 1000);
  25. }
  26. };
  27. onBeforeMount(() => {
  28. timer.value && clearInterval(timer.value);
  29. });
  30. return {
  31. count,
  32. countDown,
  33. };
  34. }
  1. <script setup lang="ts">
  2. import {useCountDown} from "@/hooks";
  3. // 倒计时
  4. const { count, countDown } = useCountDown();
  5. const sendCode = () => {
  6. console.log("发送验证码");
  7. };
  8. </script>
  9. <button :disabled="count!=0" @click="countDown(5,sendCode)">倒计时 {{ count || '' }} </button>

(2)防抖

  1. /**
  2. * @params {Function} fn 需要防抖的函数 delay 防抖时间
  3. * @returns {Function} debounce 防抖函数
  4. * @example
  5. * const { debounce } = useDebounce()
  6. * const fn = () => { console.log('防抖') }
  7. * const debounceFn = debounce(fn, 1000)
  8. * debounceFn()
  9. *
  10. */
  11. export function useDebounce() {
  12. const debounce = (fn, delay) => {
  13. let timer = null;
  14. return function () {
  15. if (timer) clearTimeout(timer);
  16. timer = setTimeout(() => {
  17. fn.apply(this, arguments);
  18. }, delay);
  19. };
  20. };
  21. return { debounce };
  22. }
  1. <script setup lang="ts">
  2. import { useDebounce } from "@/hooks";
  3. // 防抖
  4. const { debounce } = useDebounce()
  5. const fn = () => {
  6. console.log('点击了哈');
  7. }
  8. const debounceClick = debounce(fn,1000)
  9. <button @click="debounceClick">防抖点击</button>
  10. </script>

(3)节流

  1. /**
  2. * @params {Function} fn 需要节流的函数 delay 节流时间
  3. * @returns {Function} throttle 节流函数
  4. * @example
  5. * const { throttle } = useThrottle()
  6. * const fn = () => { console.log('节流') }
  7. * const throttleFn = throttle(fn, 1000)
  8. * throttleFn()
  9. *
  10. *
  11. * */
  12. export function useThrottle() {
  13. const throttle = (fn, delay) => {
  14. let timer = null;
  15. return function () {
  16. if (!timer) {
  17. timer = setTimeout(() => {
  18. fn.apply(this, arguments);
  19. timer = null;
  20. }, delay);
  21. }
  22. };
  23. };
  24. return { throttle };
  25. }
  1. <script setup lang="ts">
  2. import { useThrottle} from "@/hooks";
  3. const fn = () => {
  4. console.log('点击了哈');
  5. }
  6. const { throttle } = useThrottle()
  7. const throttleClick = throttle(fn,1000)
  8. </script>
  9. <button @click="throttleClick">节流点击</button>

后面遇到不错的hooks也会持续更新哈..,也欢迎评论出你觉得不错的hooks 函数!!!

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Cpp五条/article/detail/257358
推荐阅读
相关标签
  

闽ICP备14008679号