Vue防止按鈕重複提交
阿新 • • 發佈:2020-07-08
參考了:https://www.cnblogs.com/adbg/p/11271237.html
方法:使用全域性指令的方式。
一、新建指令
1、我們首先新建一個js檔案,例如起名為plugins.js。
2、然後我們寫入以下內容,將preventReClick指令暴露出去。
import Vue from 'vue' // 防止重複提交指令
const preventReClick = Vue.directive('preventReClick', {
inserted (el, binding) {
el.addEventListener('click', () => {
if (!el.disabled) {el.disabled = true
setTimeout(() => {
el.disabled = false
}, binding.value || 3000)
}
})
}
}) export { preventReClick }
二、main.js引用
然後我們在main.js引用這個指令,使其全域性使用。
import { preventReClick } from '@/api/tool/plugins';
三、使用
在按鈕加上v-prevent-re-click即可。
<el-button type="primary" @click="submitForm" v-prevent-re-click></el-button>