el-dialog 弹窗框确认按钮防抖处理

el-dialog 弹窗确认框按钮防抖,防抖时间是根据接口返回时间确定,防止用户多次点击多次请求接口。

el-doalog   slot="footer"  使用命名插槽 更改el-dialog默认footer部分

    <div slot="footer" class="dialog-footer">
      <div>
        <el-button size="small" @click="$emit('cancel')">取消</el-button>
        <el-button 
            size="small" 
            :disabled="confirm_Flag" 
            @click="e=>{debounceMethods(e,confirmFun);$emit('confirm')}" 
            type="primary">
            提交
        </el-button>
      </div>
    </div>

debounceMethods:防抖函数

confirmFun:promise请求函数

./debounce.js
mixins 全局混入 
export default {
  data() {
    return {
      debounceTimer: null,
    }
  },
  methods: {
    /**
     * debounceMethods 请求防抖函数
     * @param {PointerEvent} event 点击事件
     * @param {Function} func promise请求函数
     * @param {...any} args 请求参数
     * @returns 
     */
    debounceMethods(event, func, ...args) {
      let context = this
      if (typeof func !== 'function') {
        throw new Error(func + ' is not a function!')
      } else if (!event instanceof PointerEvent) {
        throw new Error(event + ' is not a PointerEvent!')
      } else if (context.debounceTimer) {
        return false
      }
      const loadingElement = document.createElement('i');
      loadingElement.setAttribute('class', 'el-icon-loading')
      loadingElement.setAttribute('style', 'margin:0 10px 0 0')
      let element = event.target
      let Button = element.tagName !== 'BUTTON' ? element.parentElement.tagName === 'BUTTON' ? element.parentElement : element : element
      Button.setAttribute('disabled',true)
      const oClass =  Button.getAttribute('class')
      if (Button.tagName === 'SPAN' ) {
        Button.setAttribute('class', `table_actionend`)
      }
      element.insertBefore(loadingElement,element.childNodes[0])
      try {
        let callback = func.apply(context, args)
        if (callback instanceof Promise) {
          context.debounceTimer = callback.finally(() => { reset() })
        } else { 
          context.debounceTimer = setTimeout(() => {reset()}, 3000)
        }
      } catch (error) {
        reset()
      }
      function reset() {
        context.debounceTimer = null;
        element.removeChild(loadingElement)
        Button.removeAttribute('disabled')
        Button.setAttribute('class', `${oClass}`)
      }
    }
}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
根据提供的引用内容,可以看出要修改el-dialog弹窗关闭按钮,可以通过以下步骤实现: 1. 在el-dialog组件中,可以使用:before-close属性来指定在关闭弹窗之前要执行的方法。在这个方法中,可以添加关闭弹窗前的逻辑,比如清空表格内容。\[1\] 2. 在el-dialog组件中,可以使用@close事件来指定在关闭弹窗时要执行的方法。在这个方法中,可以添加关闭弹窗后的逻辑,比如隐藏弹窗。\[1\] 3. 如果想要修改关闭按钮的样式,可以在样式文件中找到.el-dialog__headerbtn类,并设置background属性来修改关闭按钮的背景图。\[2\] 综上所述,要修改el-dialog弹窗关闭按钮,可以在el-dialog组件中添加:before-close属性和@close事件,并在对应的方法中添加相应的逻辑。同时,可以在样式文件中修改.el-dialog__headerbtn类来修改关闭按钮的样式。 #### 引用[.reference_title] - *1* [vue中el-dialog弹窗关闭](https://blog.csdn.net/guo950713/article/details/131063461)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [element-ui中更换el-dialog弹窗中默认的关闭按钮](https://blog.csdn.net/Shivy_/article/details/127861530)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [【前端学习记录】el-dialog弹窗点击确定按钮关闭弹窗时的执行逻辑](https://blog.csdn.net/Note_creek/article/details/129182906)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值