在vue中使用防抖函數(shù)組件操作
初級(jí)
1、先寫好防抖函數(shù)
/** * @desc 防抖函數(shù) * @param {需要防抖的函數(shù)} func * @param {延遲時(shí)間} wait * @param {是否立即執(zhí)行} immediate */export function debounce(func, wait, immediate) { let timeout return function(...args) { let context = this if (timeout) clearTimeout(timeout) if (immediate) { let callNow = !timeout timeout = setTimeout(function() { timeout = null }, wait) if (callNow) func.apply(context, args) } else { timeout = setTimeout(function() { func.apply(context, args) }, wait) } }}
2、然后在要使用的組件里 import 進(jìn)來
import { debounce } from ’xxx’export default { data: { return { vm: this } }, methods: { toDoSth: debounce((vm) => { // 這里將當(dāng)前組件實(shí)例當(dāng)參數(shù)傳入 // 就可以使用實(shí)例中定義的一些屬性、方法 // 補(bǔ)充一下,這里如果換成非箭頭函數(shù)的寫法,也可以直接訪問實(shí)例。 }, 500, true ) }}
3、在組件方法中使用
template:
<div @click='toDoSth(vm)'></div>
高級(jí)
雖然上面的寫法已經(jīng)能解決問題了,但是總覺得不夠美觀。
在網(wǎng)上搜索一番,看到有個(gè)哥們將防抖封裝成一個(gè)組件,果然和我想的一樣。不過這哥們直接將上下文當(dāng)參數(shù)傳進(jìn)來了,比我把整個(gè)實(shí)例傳進(jìn)來高明,我在這個(gè)基礎(chǔ)上添加了 immediate 的功能,還有添加了默認(rèn)不傳 event 參數(shù)的情況處理。
debounce.js 文件:
import Vue from ’vue’const debounce = (func, time, ctx, immediate) => { let timer const rtn = (...params) => { clearTimeout(timer) if (immediate) { let callNow = !timer timer = setTimeout(() => { timer = null }, time) if (callNow) func.apply(ctx, params) } else { timer = setTimeout(() => { func.apply(ctx, params) }, time) } } return rtn}Vue.component(’Debounce’, { abstract: true, props: [’time’, ’events’, ’immediate’], created() { this.eventKeys = this.events && this.events.split(’,’) }, render() { const vnode = this.$slots.default[0] // 如果默認(rèn)沒有傳 events,則對(duì)所有綁定事件加上防抖 if (!this.eventKeys) { this.eventKeys = Object.keys(vnode.data.on) } this.eventKeys.forEach(key => { vnode.data.on[key] = debounce( vnode.data.on[key], this.time, vnode, this.immediate ) }) return vnode }})
使用方式:
1、引入 debounce.js 文件
import ’xxx/debounce.js’export default { methods: { toDoSth(e) { // 這里正常寫就可以了 } }}
2、在模版里使用。
其中time為必選參數(shù)。 event 和 immediate 參數(shù)都是可選參數(shù)。
如果組件下有多個(gè)事件綁定,那么 event 可以自定義需要進(jìn)行防抖處理的事件。
如果需要立即執(zhí)行的話,可以將 immediate 參數(shù)設(shè)置為 true。
<Debounce :time='500' event='click' :immediate='true'> <button @click='toDoSth($event, 1)'>click me</button></Debounce>
到此就完成了一次 Debounce 組件的封裝。
補(bǔ)充知識(shí):vue防抖函數(shù),避免暴力點(diǎn)擊
1.vue項(xiàng)目/src/components/directive/clickAgain.js
import Vue from ’vue’const clickAgain = Vue.directive(’clickAgain’,{ // 指令的定義 bind(el, binding, vnode, oldVnode) { // 綁定this let self = vnode.context; el.onclick = function (e) { if (self._is_click) { return false; } /*執(zhí)行指令綁定的事件*/ self[binding.expression]() self._is_click=true; setTimeout(()=>{ self._is_click=false; },2000) }; }});export default clickAgain
2.在main.js 引入
import clickAgain from ’./components/directive/clickAgain.js’
/* 引入避免暴力雙擊點(diǎn)擊*/
Vue.use(clickAgain);
3.使用
<a-button key='submit' type='primary' :loading='false' v-clickAgain='handleOk'> 保存</a-button>
以上這篇在vue中使用防抖函數(shù)組件操作就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. 基于 Python 實(shí)踐感知器分類算法2. Python如何批量生成和調(diào)用變量3. ASP.NET MVC實(shí)現(xiàn)橫向展示購物車4. 通過CSS數(shù)學(xué)函數(shù)實(shí)現(xiàn)動(dòng)畫特效5. ASP.Net Core對(duì)USB攝像頭進(jìn)行截圖6. python利用opencv實(shí)現(xiàn)顏色檢測(cè)7. ASP.Net Core(C#)創(chuàng)建Web站點(diǎn)的實(shí)現(xiàn)8. Python 中如何使用 virtualenv 管理虛擬環(huán)境9. Python獲取B站粉絲數(shù)的示例代碼10. windows服務(wù)器使用IIS時(shí)thinkphp搜索中文無效問題
