vue点击按钮重复提交(思路:通过指令的方式解决)

1、src/directives/preventClick.js

import Vue from 'vue'

Vue.directive('preventClick', {
  inserted(el) {
    el.addEventListener('click', () => {
      if (!el.disabled) {
        el.disabled = true
        setTimeout(() => {
          el.disabled = false
        }, 500)
      }
    })
  }
})

 

2、main.js

    import './directives/preventClick'

 

3、使用:button按钮可以使用,500ms,从一定程度上缓解重复请求的问题

  <div id="app">
    <button v-preventClick @click="handlePuTong">普通按钮</button>
    <el-button v-preventClick @click="handleElement">Element按钮</el-button>
    <van-button v-preventClick @click="handleVant">vant按钮</van-button>
  </div>

 

posted @ 2022-04-06 16:35  吴小明-  阅读(367)  评论(0编辑  收藏  举报