vue自定义全局指令-实现防抖与节流

export default {
    install(Vue, Options) {
        // 添加全局混入
        Vue.mixin({
            mounted() {
                console.log('组件创建成功')
            },
        })
        // 添加全局过滤器
        Vue.filter('ellipsTexts', (text, num) => {
            if (text) {
                if (text.length > num) {
                    return text.substring(0, num) + '...';
                } else {
                    return text;
                }
            } else {
                return text;
            }
        })
        // 添加全局指令 只能输入数字 v-number-only
        Vue.directive('numberOnly', {
            bind(el) {
                el.handler = function () {
                    el.value = el.value.replace(/\D+/, '');
                };
                el.addEventListener('input', el.handler);
            },
            unbind(el) {
                el.removeEventListener('input', el.handler);
            }
        })
        /***
        * 防抖 单位时间只触发最后一次
        *  @param {?Number|300} time - 间隔时间
        *  @param {Function} fn - 执行事件
        *  @param {?String|"click"} event - 事件类型 例:"click"
        *  @param {Array} binding.value - [fn,event,time]
        *  例:<el-button v-debounce="[reset,`click`,300]">刷新</el-button>
        *  也可简写成:<el-button v-debounce="[reset]">刷新</el-button>
        */
        Vue.directive('debounce', {
            inserted: function (el, binding) {
                let [fn, event = "click", time = 300] = binding.value
                let timer
                el.addEventListener(event, () => {
                    timer && clearTimeout(timer)
                    timer = setTimeout(() => fn(), time)
                })
            }
        })
        /***
        *  节流 每单位时间可触发一次
        *  第一次瞬间触发,最后一次不管是否达到间隔时间依然触发
        * 【考虑到input的change事件】
        *  @param {?Number|300} time - 间隔时间
        *  @param {Function} fn - 执行事件
        *  @param {?String|"click"} event - 事件类型 例:"click"
        *  @param {Array} binding.value - [fn,event,time]
        *  例:<el-button v-throttle="[reset,`click`,300]">刷新</el-button>
        *  传递参数则:<el-button v-throttle="[()=>reset(param),`click`,300]">刷新</el-button>
        */
        Vue.directive('throttle', {
            inserted: function (el, binding) {
                let [fn, event = "click", time = 300] = binding.value
                let timer, timer_end;
                el.addEventListener(event, () => {
                    if (timer) {
                        clearTimeout(timer_end);
                        return timer_end = setTimeout(() => fn(), time);
                    }
                    fn();
                    timer = setTimeout(() => timer = null, time)
                })
            }
        })
    }
}
//在main.js中引入
import disrective from "./disrective.js"
Vue.use(disrective)
 
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值