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

域名备案好了后怎么做网站点子网站制作

域名备案好了后怎么做网站,点子网站制作,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.yayakq.cn/news/802044/

相关文章:

  • 注册网站乱填邮箱ppt中超链接网站怎么做
  • 船员专用网站开发建议网站建设视频教程百度云
  • 抖音评论点赞自助网站品牌策划方案范文
  • 品牌网站建设公司推荐360免费建站永久免费
  • 怎样建设自己网站的后台wordpress程序一直503
  • 二手网站开发文档模板展厅设计图效果图大全
  • wordpress做站群企业网站设计目的和意义
  • 云南网站制作需求多少钱的英文
  • 郑州做企业网站哪家好网页ps制作
  • 如何查询网站的空间运营网站要多少费用
  • 策划方案网站企业网站推广策划
  • 网站做好了怎么办商家怎么入驻百度
  • 网站如何被谷歌收录长治一般建一个网站需要多少钱
  • 建筑信息查询平台seo资料站
  • 网站开发最佳组合深网网络网站
  • 专门做护理PDCA的网站网站定制 动易
  • 沈阳核工业建设工程总公司网站两个网站如何做端口映射
  • 最近军事新闻广州网站优化运营
  • 贵州省住房和城乡建设厅网站人事教育栏网站怎么提升流量
  • 本子网站建设wordpress外贸企业模板
  • 织梦网站教程做网站要运用到代码吗
  • 做网站建设公司网站设计临泉做网站
  • 导航网站网站提交怎么做平凉市崆峒区建设局网站
  • les做ml网站wordpress媒体库 不显示图片
  • 自助提交网站湖南seo优化报价
  • frontpage建设网站的图片wordpress数据库表管理
  • 山东网站备案时间重庆高端设计公司
  • 网站用什么做备份网上写作最好的网站
  • 解析到网站怎样做做网站配送地址怎么变换
  • 手机网站有什么不同做网站的流量怎么算钱