vue全局注冊(cè)自定義指令防抖解析
全局注冊(cè)自定義指令防抖
1、先建一個(gè)js文件
建一個(gè)debounce.js文件,放在src/directives文件夾里面
export default (vue) => {
? /**
? ?* 綁定方法
? ?* @param {Object} el - The element the directive is bound to.
? ?* @param {Object} binding - An vue directive object
? ?*/
? ?vue.directive('debounce', { //防抖函數(shù)指令
? ? ? inserted: function(el, binding) {
? ? ? ? let timer;
? ? ? ? el.addEventListener("click", () => {
? ? ? ? ? if (timer) {
? ? ? ? ? ? clearTimeout(timer);
? ? ? ? ? }
? ? ? ? ? timer = setTimeout(() => {
? ? ? ? ? //關(guān)鍵點(diǎn):vue 的自定義指令傳遞的參數(shù)binding 如果是一個(gè)函數(shù),則通過(guò) ? ? ?binding.value()來(lái)執(zhí)行,通過(guò)上述示例,還可以傳遞比如事件, 綁定對(duì)象之類的
? ? ? ? ? ? binding.value();
? ? ? ? ? }, 1000);
? ? ? ? });
? ? ? }
? ?})
}2、在mian.js里面注冊(cè)
import Debounce from './directives/debounce.js' //防抖自定義指令 Debounce(Vue)
3、使用
在組件中button按鈕添加該指令即可實(shí)現(xiàn)防抖
v-debounce="getTableData"
vue防抖的使用
防抖函數(shù)
function debounce(fn, immediate = true) {
? let timer;
? return function () {
? ? if (timer) clearTimeout(timer);
? ? if (immediate) {
? ? ? let bool = !timer;
? ? ? timer = setTimeout(() => (timer = 0), 300);
? ? ? return bool && fn.apply(this, [...arguments]);
? ? }
? ? timer = setTimeout(() => fn.apply(this, [...arguments]), 300);
? };
}
export default {
? debounce,
};在vue中直接使用
import utils from "./utils/index";
methods:{
?? ?// 手動(dòng)添加debounce
? ? btnHandler1: utils["debounce"](function (...rest) {
? ? ? console.log("2222 ", this, rest);
? ? }),
}vue中使用高階組件
使用抽象組件對(duì)于傳入按鈕進(jìn)行改造,對(duì)于按鈕進(jìn)行事件的重寫,加入防抖功能;
import Vue from 'vue'
// ctx 【context 上下文 綁定this指向】
const debounce = (func, time, ctx, immediate = true) => {
? let timeout
? return function (...params) {
? ? if (timeout) clearTimeout(timeout)
? ? if (immediate) {
? ? ? var callNow = !timeout
? ? ? timeout = setTimeout(() => {
? ? ? ? timeout = null
? ? ? }, time)
? ? ? if (callNow) func.apply(ctx, params)
? ? } else {
? ? ? timeout = setTimeout(function () {
? ? ? ? func.apply(ctx, params)
? ? ? }, time)
? ? }
? }
}
// 只能綁定一個(gè)組件,多個(gè)組件無(wú)法綁定
Vue.component('Debounce', {
? abstract: true,//抽象組件,無(wú)狀態(tài),
? props: ['time', 'events', 'immediate'],
? created () {
? ? this.eventKeys = this.events && this.events.split(',')
? ? this.originMap = {}
? ? this.debouncedMap = {}
? },
? render () {
? ? // 組件使用proxy對(duì)象包裝,可以了解 【this】;
? ? // 取出虛擬節(jié)點(diǎn),默認(rèn)第一個(gè),也就是高階組件中若傳遞了多個(gè)子組件,只展示第一個(gè)
? ? const vnode = this.$slots.default[0]
? ? // 如果默認(rèn)沒有傳 events,則對(duì)所有綁定事件加上防抖
? ? if (!this.eventKeys) {
? ? ? this.eventKeys = Object.keys(vnode.data.on)
? ? }
? ? this.eventKeys.forEach((key) => {
? ? ? const target = vnode.data.on[key]
? ? ? if (target === this.originMap[key] && this.debouncedMap[key]) {
? ? ? ? vnode.data.on[key] = this.debouncedMap[key]
? ? ? } else if (target) {
? ? ? ? this.originMap[key] = target
? ? ? ? this.debouncedMap[key] = debounce(target, this.time, vnode, this.immediate)
? ? ? ? vnode.data.on[key] = this.debouncedMap[key]
? ? ? }
? ? })
? ? return vnode
? }
})?? ?<Debounce events="click" time="300"> ? ? ? <button @click="clickHandler(1,2,3)" :btnval="'val'">click1</button> ? ? </Debounce>
vue中自定義指令使用
// 指令【防抖】
Vue.directive("debounce", {
? // 只調(diào)用一次,第一次綁定元素時(shí)調(diào)用
? // el【綁定的元素】,binding【一個(gè)相關(guān)對(duì)象】,vnode【vue編譯生成的虛擬節(jié)點(diǎn)】
? // beforemount之后,mounted之前;
? // init events&lifecycle 【初始化事件和生命周期】
? bind(el, binding, vnode, oldVnode) {
? ? console.log(el, binding, vnode, oldVnode);
? ? let { value } = binding;
? ? let [target, time] = value;
? ? const debounced = debounce(target, time, vnode);
? ? el.addEventListener("click", debounced);
? },
? // 被綁定元素插入父節(jié)點(diǎn)時(shí)調(diào)用(僅保證父節(jié)點(diǎn)存在,但是不一定插入文檔)
? inserted() {},
? // 所在組件的vnode更新時(shí)調(diào)用
? update() {},
? componentUpdated() {},
? unbind(el) {
? ? console.log(el, "el");
? ? el.removeEventListener("click", el._debounced);
? },
});使用
<button
v-debounce="[
() => {
btnHandler();
},
300,
]"
>
點(diǎn)擊
</button>
<button v-if="testcom" v-debounce="[btnHandler, 300]">點(diǎn)擊</button>
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
- Vue3實(shí)現(xiàn)全局loading指令的示例詳解
- vue全局自定義指令和局部自定義指令的使用
- Vue全局自定義指令Modal拖拽的實(shí)踐
- vue全局自定義指令-元素拖拽的實(shí)現(xiàn)代碼
- vue directive定義全局和局部指令及指令簡(jiǎn)寫
- 對(duì)Vue2 自定義全局指令Vue.directive和指令的生命周期介紹
- vue3的自定義指令directives實(shí)現(xiàn)
- vue 自定義指令directives及其常用鉤子函數(shù)說(shuō)明
- vue?filters和directives訪問(wèn)this的問(wèn)題詳解
- vue通過(guò)指令(directives)實(shí)現(xiàn)點(diǎn)擊空白處收起下拉框
- 詳解vue + vuex + directives實(shí)現(xiàn)權(quán)限按鈕的思路
- vue全局指令文件?directives詳解
相關(guān)文章
使用vue cli4.x搭建vue項(xiàng)目的過(guò)程詳解
這篇文章主要介紹了使用vue cli4.x搭建vue項(xiàng)目的過(guò)程,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-05-05
Vue自定義省市區(qū)三級(jí)聯(lián)動(dòng)
這篇文章主要為大家詳細(xì)介紹了Vue自定義省市區(qū)三級(jí)聯(lián)動(dòng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-03-03
vue事件監(jiān)聽函數(shù)on中的this指針域使用
這篇文章主要介紹了vue事件監(jiān)聽函數(shù)on中的this指針域使用方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-08-08
vant中的Cascader級(jí)聯(lián)選擇異步加載地區(qū)數(shù)據(jù)方式
這篇文章主要介紹了vant中的Cascader級(jí)聯(lián)選擇異步加載地區(qū)數(shù)據(jù)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-07-07
vue項(xiàng)目報(bào)錯(cuò):Missing?script:"serve"的解決辦法
這篇文章主要給大家介紹了關(guān)于vue項(xiàng)目報(bào)錯(cuò):Missing?script:"serve"的解決辦法,"missing script: serve"是一個(gè)錯(cuò)誤信息,意味著在執(zhí)行啟動(dòng)腳本時(shí),找不到名為"serve"的腳本,需要的朋友可以參考下2023-11-11
vue打包優(yōu)化時(shí)配置webpack的8大方案小結(jié)
vue-cli?生成的項(xiàng)目通常集成Webpack?,在打包的時(shí)候,需要webpack來(lái)做一些事情,這里我們希望它可以壓縮代碼體積,提高運(yùn)行效率,本文為大家整理了8大webpack配置方案,希望對(duì)大家有所幫助2024-02-02
vue?this.$router和this.$route區(qū)別解析及路由傳參的2種方式?&&?this.$route
this.$router?相當(dāng)于一個(gè)全局的路由器對(duì)象,包含了很多屬性和對(duì)象(比如?history?對(duì)象),任何頁(yè)面都可以調(diào)用其?push(),?replace(),?go()?等方法,本文給大家介紹Vue中this.$router與this.$route的區(qū)別?及push()方法,感興趣的朋友跟隨小編一起看看吧2023-10-10
如何解決ElementUI導(dǎo)航欄重復(fù)點(diǎn)菜單報(bào)錯(cuò)問(wèn)題
這篇文章主要介紹了如何解決ElementUI導(dǎo)航欄重復(fù)點(diǎn)菜單報(bào)錯(cuò)問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-07-07

