当前位置: 首页 > news >正文

无锡做网站的公司电话百度推广在哪里能看到

无锡做网站的公司电话,百度推广在哪里能看到,东莞工作装制作,自贡建设能源开发有限公司网站hooks 是什么 vue3使用了composition API,我们可自定义封装hooks,达到复用,在Vue2中采取的mixins,对mixins而言, hooks更清楚复用功能代码的来源, 更清晰易懂。 简单来说:hooks 就是函数的一种写法&#xf…

hooks 是什么

vue3使用了composition API,我们可自定义封装hooks,达到复用,在Vue2中采取的mixins,对mixins而言, hooks更清楚复用功能代码的来源, 更清晰易懂。

简单来说:hooks 就是函数的一种写法,就是将文件的一些单独功能的 js 代码进行抽离出来进行封装使用 

 自定义 hook 需要满足的规范

具备可复用功能,才需要抽离为 hooks 独立文件

函数名前缀加上use开头,形如: useXX

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

暴露出 变量 或 方法 提供外部需要时使用

 Hooks 常用 工具

src/hooks/index.ts

倒计时hooks 

import { ref, Ref, onBeforeMount } from 'vue';/***  倒计时hooks*  @param {Number} second 倒计时秒数*  @return {Number} count 倒计时秒数*  @return {Function} countDown 倒计时函数*  @example*  const { count, countDown } = useCountDown()*  countDown(60)*  <div>{{ count }}</div>*/export function useCountDown() {// 定义函数参数和返回值类型const count: Ref<number> = ref(0);const timer: Ref<NodeJS.Timeout | null> = ref(null);const countDown = (second: number, callback?: () => void) => {if (count.value === 0 && timer.value === null) {// 如果提供了回调函数,则调用它if (callback) callback();count.value = Number(second);timer.value = setInterval(() => {count.value--;if (count.value === 0) {clearInterval(timer.value!);timer.value = null;}}, 1000);}};onBeforeMount(() => {// 确保 timer 有值再清除timer.value && clearInterval(timer.value);});return {count,countDown,};
}
<template><button :disabled="count !== 0" @click="countDown(3, logCountdown)">倒计时剩余 {{ `${count}秒` }} </button>
</template><script lang="ts" setup name="HomePage">import { useCountDown } from '@/hooks';// 倒计时const { count, countDown } = useCountDown();const logCountdown = () => {console.log('触发倒计时回调函数');};
</script>

防抖hooks

import { ref, Ref, onBeforeMount } from 'vue';/*** 防抖hooks* @params {Function} fn  需要防抖的函数 delay 防抖时间* @returns {Function} debounce 防抖函数* @example* const { debounce } = useDebounce()* const fn = () => { console.log('防抖') }* const debounceFn = debounce(fn, 1000)* debounceFn()*/// 防抖函数的类型
interface DebounceFunction {(fn: (...args: any[]) => void, delay: number): (...args: any[]) => void;
}export function useDebounce(): { debounce: DebounceFunction } {const debounce: DebounceFunction = (fn, delay) => {let timer: ReturnType<typeof setTimeout> | null = null;return function (this: any, ...args: any[]) {if (timer) clearTimeout(timer);timer = setTimeout(() => {fn.apply(this, args);}, delay);};};return { debounce };
}
<template><button @click="debounceClick">防抖点击</button>
</template><script lang="ts" setup name="HomePage">import { useDebounce } from '@/hooks';// 防抖const { debounce } = useDebounce();const fn = () => {console.log('取消点击一秒后打印');};const debounceClick = debounce(fn, 1000);
</script>

节流hooks

import { ref, Ref, onBeforeMount } from 'vue';/*** 节流hooks* @params {Function} fn  需要节流的函数 delay 节流时间* @returns {Function} throttle 节流函数* @example* const { throttle } = useThrottle()* const fn = () => { console.log('节流') }* const throttleFn = throttle(fn, 1000)* throttleFn()*/// 节流函数的类型
interface ThrottleFunction {(fn: (...args: any[]) => void, delay: number): (...args: any[]) => void;
}export function useThrottle(): { throttle: ThrottleFunction } {const throttle: ThrottleFunction = (fn, delay) => {let timer: ReturnType<typeof setTimeout> | null = null;return function (this: any, ...args: any[]) {if (!timer) {timer = setTimeout(() => {fn.apply(this, args);timer = null;}, delay);}};};return { throttle };
}
<template><button @click="throttleClick">节流点击</button>
</template><script lang="ts" setup name="HomePage">import { useThrottle } from '@/hooks';const fn = () => {console.log('一秒内只打印一次');};const { throttle } = useThrottle();const throttleClick = throttle(fn, 1000);
</script>
http://www.wooajung.com/news/23720.html

相关文章:

  • 网站建设公司中心网页模板免费html
  • 苏州企业建站系统模板百度指数查询网
  • 广安网站开发seo内容优化方法
  • 微信公众号公众平台网站为什么要做seo
  • 怎么做淘宝客网站做淘客拼多多代运营一般多少钱
  • 凡网站建设深圳推广
  • 南城区做网站seo优化咨询
  • 域名续费后网站打不开百度邮箱登录入口
  • 只做衬衣网站百度seo排名优化技巧分享
  • 织梦做的网站图片路径在哪淘客推广怎么做
  • 政府网站建设方案范文—工作方案seo专员岗位职责
  • 北京建设工程教育中心网站平台推广方案
  • 高端做网站哪家好临沂seo
  • 深圳创业补贴政策2022申请条件网站排名优化制作
  • 网页设计实训报告总结100字北京seo优化推广
  • 利用切片做网站背景图片域名访问网站怎么进入
  • 闸北专业做网站网络推广外包公司干什么的
  • 郑州网站建设最低价网络营销运营方案
  • 预约做家庭清洁的网站西安网站建设制作公司
  • 怎么制作网站教程图片石家庄seo按天扣费
  • 织梦网站优化教程开个网站平台要多少钱
  • wordpress主题官网seo关键词大搜
  • 搜索引擎优化教程seo词条
  • 东莞机械建站如何培训机构网站
  • 做调查赚钱的网站有哪些南宁优化网站收费
  • 中国企业500强搜索版宜昌网站seo收费
  • 哪里做网站排名职业培训机构
  • 网站 多线域名注册好了怎么弄网站
  • 寻找郑州网站建设网站制作维护
  • 网站建设内存网络推广营销策划方案