vue vue3 节流 封装全局 自定义全局 节流


前言

一、节流是什么?

"节流"是一种控制事件触发频率的技术。当某个事件被触发时,节流能够确保事件处理函数不会立即执行,而是在一定的时间间隔之后执行。这有助于减少事件处理函数的执行次数,特别是在处理频繁触发的事件(比如滚动、调整窗口大小、输入框输入等)时,以提高性能和减轻不必要的计算负担。

二、使用步骤

1.创建文件

#创建throttle.js
const throttle = (fn, delay) => {
  let last = 0
  return function () {
    const now = Date.now()
    if (now - last >= delay) {
      fn.apply(this, arguments)
      last = now
    }
  }
}

export default {
  mounted(el, binding) {
    const { value, arg } = binding
    const delay = arg ? parseInt(arg, 10) : 1000

    const throttledClick = throttle(value, delay)

    el.__throttledClick__ = throttledClick

    el.addEventListener('click', throttledClick)
  },
  beforeUnmount(el) {
    console.log('Before unmount')
    if (el) {
      console.log(el)
      console.log(el.__throttledClick__)
      el.removeEventListener('click', el.__throttledClick__)
      delete el.__throttledClick__
    }
  }
}

2.全局定义

import throttleDirective from '@/utils/throttle' // 注册全局指令
app.directive('throttle', throttleDirective)

在这里插入图片描述


3.全局使用

//这里面默认1000   可自定义时间 
	<el-button  type="primary" v-throttle="opneDialog"> 添加机构 </el-button>
	<el-button  type="primary" v-throttle:3000="opneDialog"> 添加机构 </el-button>

!!!注意事项

// 这种写法 需要把单独写为方法 才可以正常使用
   <el-button  @click="formVisible = true"> 添加机构 </el-button>
  • 29
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Vue 3中结合TypeScript封装全局的防抖和节流函数可以通过创建一个`utils`文件,并在Vue应用的入口文件中进行全局注册。下面是一个示例: ```typescript // utils/debounce.ts export function debounce(func: Function, delay: number) { let timer: NodeJS.Timeout | null = null; return function(...args: any[]) { if (timer) { clearTimeout(timer); } timer = setTimeout(() => { func.apply(this, args); }, delay); }; } // utils/throttle.ts export function throttle(func: Function, delay: number) { let timer: NodeJS.Timeout | null = null; let lastRun = 0; return function(...args: any[]) { const currentTime = Date.now(); if (timer) { clearTimeout(timer); } if (currentTime - lastRun >= delay) { func.apply(this, args); lastRun = currentTime; } else { timer = setTimeout(() => { func.apply(this, args); lastRun = Date.now(); }, delay); } }; } ``` 然后,在Vue应用的入口文件(如`main.ts`)中,可以将这些函数注册为全局方法: ```typescript import { createApp } from 'vue'; import App from './App.vue'; import { debounce, throttle } from './utils'; const app = createApp(App); app.config.globalProperties.$debounce = debounce; app.config.globalProperties.$throttle = throttle; app.mount('#app'); ``` 现在,你可以在Vue组件中使用`this.$debounce`和`this.$throttle`来调用防抖和节流函数了。 ```vue <template> <div> <input type="text" @input="handleInput" /> </div> </template> <script> export default { methods: { handleInput: this.$debounce(function(event) { console.log(event.target.value); }, 300), }, }; </script> ``` 在上面的示例中,`handleInput`方法使用了防抖函数,延迟300毫秒后执行。你可以根据实际需求调整防抖和节流的延迟时间。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

YOIO.

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值