- 项目中创建directive文件夹,用来存放自定义指令,新建index.js
-
export default { install(Vue) { // 禁止短时间内重复点击 Vue.directive("preventClick", { inserted(button, bind) { button.addEventListener("click", () => { if (!button.disabled) { button.disabled = true; setTimeout(() => { button.disabled = false; }, 1000); } }); } }); } };
-
main.js中引入,全局使用
-
import preventClick from './directive/index' Vue.use(preventClick)
-
在需要使用的按钮上面加上自定义指令
-
<button v-preventClick>登录</button>
Vue自定义指令 限制按钮短时间内重复提交
最新推荐文章于 2023-07-26 13:46:56 发布
本文介绍了如何在Vue项目中创建一个名为'preventClick'的自定义指令,用于限制按钮的连续点击,防止短时间内重复操作。通过在main.js中导入并全局注册该指令,开发者可以方便地在需要的按钮上添加此功能。
摘要由CSDN通过智能技术生成