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

查询备案网站腾讯nba新闻

查询备案网站,腾讯nba新闻,设计素材模板,安卓手机建网站一、前言:为什么需要防抖节流? 在前端开发中,高频触发的事件(如滚动、输入、点击等)容易导致性能问题。防抖(debounce) 和 节流(throttle) 是两种常用的优化手段&#x…

一、前言:为什么需要防抖节流?

在前端开发中,高频触发的事件(如滚动、输入、点击等)容易导致性能问题。防抖(debounce)节流(throttle) 是两种常用的优化手段:

  • 防抖:事件触发后等待指定时间再执行,若期间重复触发则重新计时
  • 节流:事件触发后立即执行,并在指定时间内不再响应新触发

本文将教你如何在 Vue3 + TypeScript 项目中封装一个灵活可复用的防抖/节流自定义指令。


二、实现思路分析

1. Vue3 自定义指令基础

Vue3 提供了 app.directive() 方法注册全局指令,生命周期包含:

  • mounted:元素挂载时
  • updated:元素更新时
  • unmounted:元素卸载时

2. 设计目标

  • 支持防抖/节流模式切换
  • 可自定义延迟时间
  • TypeScript 类型支持
  • 自动清除事件监听

三、完整代码实现

1. 创建指令文件 directives/debounceThrottle.ts

import type { App, Directive, DirectiveBinding } from 'vue'type ExecutableFunction = (...args: any[]) => void
type Strategy = 'debounce' | 'throttle'interface DirectiveOptions {strategy?: Strategydelay?: number
}// 策略模式实现
const strategyImplement = {debounce(fn: ExecutableFunction, delay: number) {let timer: NodeJS.Timeout | null = nullreturn (...args: any[]) => {if (timer) clearTimeout(timer)timer = setTimeout(() => fn(...args), delay)}},throttle(fn: ExecutableFunction, delay: number) {let lastExecTime = 0return (...args: any[]) => {const now = Date.now()if (now - lastExecTime >= delay) {fn(...args)lastExecTime = now}}}
}const debounceThrottleDirective: Directive = {mounted(el: HTMLElement, binding: DirectiveBinding<ExecutableFunction>) {const { value: fn } = bindingconst { strategy = 'debounce', delay = 300 }: DirectiveOptions = binding.modifiers || {}if (typeof fn !== 'function') {throw new Error('v-debounce-throttle requires a function as the value')}// 通过策略模式选择实现const executor = strategyImplement[strategy](fn, delay)// 存储到元素属性以便后续更新和卸载el._debounceThrottleHandler = executorel.addEventListener('click', executor)},updated(el, binding) {// 当参数变化时重新绑定el.removeEventListener('click', el._debounceThrottleHandler)debounceThrottleDirective.mounted(el, binding)},unmounted(el) {el.removeEventListener('click', el._debounceThrottleHandler)delete el._debounceThrottleHandler}
}// 扩展HTMLElement类型声明
declare global {interface HTMLElement {_debounceThrottleHandler?: (...args: any[]) => void}
}export function setupDebounceThrottleDirective(app: App) {app.directive('debounce-throttle', debounceThrottleDirective)
}

2. 在 main.ts 中注册

import { createApp } from 'vue'
import App from './App.vue'
import { setupDebounceThrottleDirective } from './directives/debounceThrottle'const app = createApp(App)
setupDebounceThrottleDirective(app)
app.mount('#app')

四、使用示例

1. 基础用法(默认防抖300ms)

<template><button v-debounce-throttle="handleClick">提交</button>
</template>

2. 指定节流模式

<button v-debounce-throttle.throttle="handleScroll">滚动处理</button>

3. 自定义延迟时间

<input v-debounce-throttle:input.throttle="handleInput"@input="(e) => $emit('update:modelValue', e.target.value)"
/>

五、关键实现解析

  1. 策略模式:通过对象字面量实现不同策略的快速切换
  2. 类型安全:使用TS接口规范参数类型
  3. 内存管理:在unmounted阶段自动移除监听
  4. 参数更新处理:updated生命周期实现动态参数更新
  5. 元素属性扩展:通过HTMLElement接口扩展存储处理方法

六、常见应用场景

  1. 搜索框输入联想(防抖)
  2. 防止按钮重复点击(节流)
  3. 滚动事件处理(节流)
  4. 窗口resize事件(防抖)
  5. canvas绘图高频事件(节流)

七、总结

通过封装这个自定义指令,我们实现了:

  • ✅ 统一的防抖/节流处理逻辑
  • ✅ 灵活的策略和参数配置
  • ✅ 完善的TS类型支持
  • ✅ 自动化的内存管理

在项目中合理使用可以有效提升应用性能,同时保持代码的整洁和可维护性。后续可以继续扩展支持更多事件类型和配置项,打造更强大的指令库。

http://www.rdtb.cn/news/961.html

相关文章:

  • 网站seo报表seo全网推广营销软件
  • 企业网站建设方案论文长春网站关键词排名
  • 从留言板开始做网站建站宝盒
  • 免费tickle网站it行业培训机构哪个好
  • 网站建设jiqb站推广网站入口
  • 域名除了做网站还能做什么广州百度推广代理公司
  • 做网站用java关键词爱站网关键词挖掘工具
  • 网站更新怎么做seo企业推广案例
  • 简单的网站建设网络营销的基本功能
  • 网站开发中的paml网站设计制作哪家好
  • 推荐软件分类seo服务公司上海
  • 网站网页设计原则t和p在一起怎么做网站
  • 深圳商城网站开发优秀网站设计
  • 个人可以做商城网站百度推广找谁做靠谱
  • 网站 目录结构百度推广电话号码
  • jsp网站开发论文做市场推广应该掌握什么技巧
  • ui设计需要学什么软件seo是什么意思呢
  • 自由型网站永久免费的电销外呼系统
  • 男生女生做羞羞事的网站网站收录怎么弄
  • 北京西站附近景点网络推广优化平台
  • 常见的域名注册网站企业网站推广效果指标分析
  • 潍坊网站建设公司哪家好公司官网优化方案
  • 大连网站哪家做的好网络广告案例以及分析
  • 免费建个人手机网站搜索引擎优化方法
  • 中色冶金建设有限公司网站泉州百度seo公司
  • 做暖漫画网站制作网站平台
  • 编程机构搜索引擎排名优化seo
  • wordpress数据连接失败如何做seo整站优化
  • 专门做书籍设计的网站南宁网站推广大全
  • 加工厂网站建设搜一搜搜索