1.新建 directive.js
import Vue from 'vue'
const preventReClick = Vue.directive('preventReClick', {
inserted: function (el, binding) {
el.addEventListener('click', () => {
if (!el.disabled) {
el.disabled = true
setTimeout(() => {
el.disabled = false
}, binding.value || 3000)
}
})
}
});
export { preventReClick }
2.在main.js中引入
import preventReClick from './utils/directive' //防止多次点击,重复提交
3.设置按钮重复点击,在按钮上添加v-preventReClick
<el-button type="primary" @click="type=='add'? confirmAdd() : confirmUpdate()" v-preventReClick>确 定</el-button>