阻止快速點擊按鈕會重復多次調用接口的


阻止快速點擊按鈕會重復多次調用接口的

摘抄自: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>

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM