Vue自定义指令 限制按钮短时间内重复提交

8 篇文章 0 订阅
本文介绍了如何在Vue项目中创建一个名为'preventClick'的自定义指令,用于限制按钮的连续点击,防止短时间内重复操作。通过在main.js中导入并全局注册该指令,开发者可以方便地在需要的按钮上添加此功能。
摘要由CSDN通过智能技术生成
  1. 项目中创建directive文件夹,用来存放自定义指令,新建index.js
  2. export default {
      install(Vue) {
        // 禁止短时间内重复点击
        Vue.directive("preventClick", {
          inserted(button, bind) {
            button.addEventListener("click", () => {
              if (!button.disabled) {
                button.disabled = true;
                setTimeout(() => {
                  button.disabled = false;
                }, 1000);
              }
            });
          }
        });
      }
    };
    

     

  3. main.js中引入,全局使用

  4. import preventClick from './directive/index'
    Vue.use(preventClick)

     

  5. 在需要使用的按钮上面加上自定义指令

  6. <button v-preventClick>登录</button>

     

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值