防止按钮多次点击 preventReClick
1. 创建 preventReClick.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 || 800) } }) } }); export { preventReClick }
2. 在main中引入
import '@/utils/preventReClick'
3. html文件中使用
<el-button type="primary" @click="foo" v-preventReClick>按钮</el-button>