阻止快速點擊按鈕會重復多次調用接口的
摘抄自: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>