阻止快速点击按钮会重复多次调用接口的

阻止快速点击按钮会重复多次调用接口的

摘抄自:https://blog.csdn.net/u013179425/article/details/107483482,因为怕别人删除了找不到所以自己复制下来记录一下,我已经试过了,这个方法非常好。

  • 定义全局指令
// directive.js
export default {
  install (Vue) {
    // 防重复点击(指令实现)
    Vue.directive('repeatClick', {
      inserted (el, binding) {
        el.addEventListener('click', () => {
          if (!el.disabled) {
            el.disabled = true
            setTimeout(() => {
              el.disabled = false
            }, binding.value || 1000)
          }
        })
      }
    })
  }
}

在main.js引用

import directive from 'directive.js';
vue.use(directive );

按钮调用直接加v-repeatClick

<el-button v-repeatClick type="prismary" style="width:100%;" @click="handleSubmit"></el-button>

 

posted @ 2020-08-04 18:08  sinceForever  阅读(873)  评论(0编辑  收藏  举报