解决Element 中 Message 消息提示 连续点击 多弹窗问题

问题如图所示:
在这里插入图片描述
预期解决效果: 每次只弹出一个。

解决办法: 重写 $message

直接上代码

// resetMessage.js
/*
* 重写this.$message, 防止连续点击多次弹窗
* 调用样例: this.$message.info('xxx')
* */
import {Message} from 'element-ui';
const showMessage = Symbol('showMessage');
class DonMessage {
    success (options, single = true) {
        this[showMessage]('success', options, single)
    }
    warning (options, single = true) {
        this[showMessage]('warning', options, single)
    }
    info (options, single = true) {
        this[showMessage]('info', options, single)
    }
    error (options, single = true) {
        this[showMessage]('error', options, single)
    }

    [showMessage] (type, options, single) {
        if (single) {
            // 判断是否已存在Message
            if (document.getElementsByClassName('el-message').length === 0) {
                // 修改参数
                if(type === 'error' || type === 'warning'){
                    options = {
                        message: options,
                        duration: 3500,
                        showClose: true
                    };
                } else {
                    options = {
                        message: options,
                        duration: 2000,
                        showClose: true
                    };
                }
                Message[type](options)
            }
        } else {
            options = {
                message: options,
                duration: 2000,
                showClose: true
            };
            Message[type](options)
        }
    }
}
export const newMessage = new DonMessage();

重写后在main.js 中覆盖:

//main.js
import {newMessage} from  '@/assets/js/resetMessage.js';
// 重写$message
Vue.prototype.$message = newMessage;

调用样例:

this.$message.success('操作成功');

结果: 重写后可完美解决此问题。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
要在 Element UI 的 message 消息提示添加按钮和点击事件,可以使用 Element UI 提供的 $message 方法,同时传入一个配置对象来实现。 以下是一个示例代码,创建一个带有按钮和点击事件的消息提示: ``` this.$message({ message: '这是一条带有按钮和点击事件的消息', duration: 0, showClose: true, center: true, customClass: 'message-box', type: 'warning', offset: 100, dangerouslyUseHTMLString: true, iconClass: 'el-icon-warning', customClass: 'message-box', showCancelButton: true, cancelButtonText: '取消', confirmButtonText: '确定', callback: action => { if (action === 'confirm') { // 点击确定按钮 console.log('点击了确定按钮') } else if (action === 'cancel') { // 点击取消按钮 console.log('点击了取消按钮') } } }) ``` 在这个示例,我们使用了 Element UI 的 $message 方法来创建一个消息提示,同时传入一个配置对象。其,我们设置了以下几个配置项: - `message`:消息提示的内容。 - `duration`:消息提示的持续时间。这里设置为 0,表示该消息提示不会自动关闭,需要点击按钮才能关闭。 - `showClose`:是否显示关闭按钮。这里设置为 true,表示该消息提示会显示一个关闭按钮。 - `center`:是否居显示。这里设置为 true,表示该消息提示会在屏幕央显示。 - `customClass`:自定义样式类名。这里设置为 message-box,表示该消息提示会应用一个名为 message-box 的样式。 - `type`:消息提示的类型。这里设置为 warning,表示该消息提示是一个警告类型。 - `offset`:消息提示的偏移量。这里设置为 100,表示该消息提示会在垂直方向上向下偏移 100 像素。 - `dangerouslyUseHTMLString`:是否将 message 字段作为 HTML 片段处理。这里设置为 true,表示 message 字段可以包含 HTML 标签。 - `iconClass`:自定义图标类名。这里设置为 el-icon-warning,表示该消息提示会使用一个名为 el-icon-warning 的图标。 - `showCancelButton`:是否显示取消按钮。这里设置为 true,表示该消息提示会显示一个取消按钮。 - `cancelButtonText`:取消按钮的文本。这里设置为 取消。 - `confirmButtonText`:确定按钮的文本。这里设置为 确定。 - `callback`:按钮点击后的回调函数。该函数会接收一个参数 action,表示用户点击的按钮。如果 action 等于 confirm,表示用户点击了确定按钮;如果 action 等于 cancel,表示用户点击了取消按钮。 在回调函数,我们可以根据用户点击的按钮执行相应的操作。在这个示例,我们只是简单地在控制台输出了一条消息。你可以根据自己的需求来编写具体的逻辑。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

qq_1075654056

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值