vue 如何處理防止按鈕重復(fù)點(diǎn)擊問題
處理防止按鈕重復(fù)點(diǎn)擊
1.在button上綁定動(dòng)態(tài)的disabled
<el-button type="primary" size="mini" @click="testCode" :disabled="codeDisabled">發(fā)送驗(yàn)證碼</el-button>
2.在data中定義
codeDisabled: false,
3.在點(diǎn)擊事件里加入定時(shí)器,60000為1分鐘不能重復(fù)點(diǎn)擊
?testCode() {
? ? ? this.codeDisabled = true
? ? ? setTimeout(()=>{
? ? ? ? this.codeDisabled = false;
? ? ? },60000)
? }vue防止重復(fù)執(zhí)行點(diǎn)擊事件
在vue項(xiàng)目中防止用戶在一定時(shí)間內(nèi)頻繁點(diǎn)擊按鈕觸發(fā)事件
方法一:在規(guī)定時(shí)間內(nèi)將按鈕禁用的方法
主要思想就是禁止用戶在一定的時(shí)間多次點(diǎn)擊,在一定時(shí)間內(nèi)將按鈕禁用,用定時(shí)器實(shí)現(xiàn),一定時(shí)間之后用戶可再次點(diǎn)擊。
<template>
? <div>
? ? ? <div @click="clickHandle()">我是點(diǎn)擊事件</div>
? </div>
</template>
?
<script>
export default {
? components: {},
? data () {
? ? return {
? ? ? isDisabled: false,
? ? };
? },
? methods: {
? ? ? clickHandle(){
? ? ? ? ? this.isDisabled = true;
? ? ? ? ? setTimeout(()=>{
? ? ? ? ? ? ? this.isDisabled = false;
? ? ? ? ? },3000)
? ? ? }
? },
}
</script>方法二:用指令的方式實(shí)現(xiàn),全局注冊
export default {
? ? install(Vue) {
? ? ? ? // 防止重復(fù)點(diǎn)擊
? ? ? ? Vue.directive('preventReClick', {
? ? ? ? ? ? inserted(el, binding) {
? ? ? ? ? ? ? ? el.addEventListener('click', () => {
? ? ? ? ? ? ? ? ? ? if (!el.disabled) {
? ? ? ? ? ? ? ? ? ? ? ? el.disabled = true;
? ? ? ? ? ? ? ? ? ? ? ? setTimeout(() => {
? ? ? ? ? ? ? ? ? ? ? ? ? ? el.disabled = false;
? ? ? ? ? ? ? ? ? ? ? ? }, binding.value || 1000)
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? })
? ? ? ? ? ? }
? ? ? ? })
? ? }
}1. 在main.js中引入上面的js文件
// 防止多次點(diǎn)擊 import preventReClick from './common/utils/utils' Vue.use(preventReClick);
2. 在觸發(fā)事件的按鈕上就可以直接使用指令
<div class="comment-btn" @click="submitMes()" v-preventReClick="3000">發(fā)送</div>
3. 3秒之后 按鈕下面的事件才可再次觸發(fā)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
vue-cli4創(chuàng)建項(xiàng)目導(dǎo)入Element-UI踩過的坑及解決
這篇文章主要介紹了vue-cli4創(chuàng)建項(xiàng)目導(dǎo)入Element-UI踩過的坑及解決,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-04-04
vue打開其他項(xiàng)目頁面并傳入數(shù)據(jù)詳解
這篇文章主要給大家介紹了關(guān)于vue打開其他項(xiàng)目頁面并傳入數(shù)據(jù)的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-11-11
vue項(xiàng)目使用js監(jiān)聽瀏覽器關(guān)閉、刷新及后退事件的方法
這篇文章主要給大家介紹了關(guān)于vue項(xiàng)目使用js監(jiān)聽瀏覽器關(guān)閉、刷新及后退事件的相關(guān)資料,文中通過代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用vue具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-09-09
vue2 router 動(dòng)態(tài)傳參,多個(gè)參數(shù)的實(shí)例
下面小編就為大家?guī)硪黄獀ue2 router 動(dòng)態(tài)傳參,多個(gè)參數(shù)的實(shí)例。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-11-11

