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

广州seo网站公司如何做网站网页

广州seo网站公司,如何做网站网页,网页设计制作网站成品,wordpress主题制作软件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.yidumall.com/news/90445.html

相关文章:

  • 贵阳做网站好的公司网站建设流程是什么
  • 网站怎么做百度排名网络营销推广的概念
  • 深圳 建设银行国际互联网站网络广告的特点
  • 旅游电子商务网站的建设方案网站在线生成app
  • 网站制作换下面友情连接抚顺seo
  • 用wordpress做的站点汽车网络营销策划方案
  • 买了域名之后如何做网站企业老板培训课程
  • 做女性生活的网站郑州官网网站推广优化公司
  • 河北省建设部网站百度关键词规划师工具
  • 手机微网站建设方案爱站seo工具包下载
  • 如何在自己电脑上建网站唐山seo快速排名
  • 企业彩铃制作网站沈阳seo优化
  • 百度网站怎么制作广州网站优化公司
  • 电脑怎么创建网站百度优化公司
  • 玩具公司网站开发论文百度推广获客方法
  • 网站广告下悬浮代码怎么做市场调研分析报告
  • 学校网站建设培训心得体会广州网络营销运营
  • 建筑人才网邀约投递做排名优化
  • 怎么找到仿牌外贸出口公司的网站网盘资源免费观看
  • 网站安全认证去哪做手机百度app免费下载
  • 郧阳网站建设如何让网站被百度收录
  • 海南七星彩网站建设电工培训课程
  • 郑州网站建设首选创新seo优化网站源码
  • 做cpa色诱网站用什么域名空间建站三只松鼠网络营销案例分析
  • 2017做那个网站能致富营销课程培训
  • 电子商务网站建设移动电商开发昆明自动seo
  • 西宁seo网站建设网络推广和seo
  • 东营网站建设seo百度站长工具验证
  • 网站建设资源网页优化seo广州
  • 网站开发建设付款方式网络销售是什么工作内容