偷偷摘套内射激情视频,久久精品99国产国产精,中文字幕无线乱码人妻,中文在线中文a,性爽19p

ElementUI 提示框單例控制:從 2.x 到 3.x 的完整解決方案

開發(fā) 前端
本文將深入探討 ElementUI 2.x 和 3.x 如何實(shí)現(xiàn) 單例提示框,并提供優(yōu)秀實(shí)踐方案。

在前端開發(fā)中,ElementUI 的 Message(消息提示) 和 Notification(通知框) 是高頻使用的組件。但在某些場景下,我們不希望它們 同時(shí)彈出多個(gè),比如:

  • 表單提交,防止重復(fù)提示
  • 全局錯(cuò)誤攔截,避免堆疊報(bào)錯(cuò)
  • 關(guān)鍵操作反饋,確保用戶不被干擾

本文將深入探討 ElementUI 2.x 和 3.x 如何實(shí)現(xiàn) 單例提示框,并提供 最佳實(shí)踐方案。

一、 ElementUI 2.x 的解決方案

2.x 版本對(duì) Message 和 Notification 的支持不同,需分別處理。

1. Notification(支持 max 配置)

Notification 在 2.X 版本支持全局 max 配置,可限制最大顯示數(shù)量:

import Vue from 'vue';
import ElementUI from 'element-ui';

Vue.use(ElementUI, {
  notification: {
    max: 1,  // 同一時(shí)間只顯示一個(gè)
    offset: 20,  // 位置偏移
    duration: 3000  // 自動(dòng)關(guān)閉時(shí)間
  }
});

優(yōu)點(diǎn):

  • 配置簡單,無需額外代碼
  • 自動(dòng)關(guān)閉舊彈窗,確保唯一性

缺點(diǎn):? 僅適用于 Notification,Message 不支持

2. Message(需手動(dòng)封裝單例)

Message 在 2.x **不支持 max**,必須手動(dòng)控制:

方案 1:全局封裝單例 Message

import { Message } from'element-ui';

let activeMessage = null;

exportfunction showSingleMessage(options) {
if (activeMessage) {
    activeMessage.close(); // 關(guān)閉舊消息
  }
  activeMessage = Message(options);

// 監(jiān)聽關(guān)閉,防止內(nèi)存泄漏
const originalOnClose = options.onClose || (() => {});
  options.onClose = () => {
    originalOnClose();
    activeMessage = null;
  };
}

// 使用
showSingleMessage({ message: '操作成功', type: 'success' });

方案 2:Vue 原型掛載(全局調(diào)用)

// main.js
import { Message } from'element-ui';

let currentMessage = null;

Vue.prototype.$singleMessage = (options) => {
if (currentMessage) currentMessage.close();
  currentMessage = Message({
    ...options,
    onClose: () => {
      currentMessage = null;
      options.onClose?.();
    }
  });
};

// 組件內(nèi)調(diào)用
this.$singleMessage({ message: '僅顯示我', type: 'warning' });

適用場景:

  • 需要全局控制 Message 唯一性
  • 防止重復(fù)提交、錯(cuò)誤堆疊

二、ElementUI 3.x(Element Plus)的優(yōu)化

Element Plus(基于 Vue 3)對(duì) Message 和 Notification 都支持 max 配置,使用更簡單。

1. 全局配置 max

import { createApp } from'vue';
import ElementPlus from'element-plus';

const app = createApp(App);

app.use(ElementPlus, {
// Message 配置
message: {
    max: 1,  // 限制僅顯示一個(gè)
  },
// Notification 配置
notification: {
    max: 1,
  },
});

優(yōu)點(diǎn):

  • 一行配置解決單例問題
  • 適用于 Message 和 Notification

2. 動(dòng)態(tài)控制(高級(jí)用法)

如果某些場景需要 臨時(shí)關(guān)閉單例限制,可以動(dòng)態(tài)調(diào)整:

import { ElMessage } from 'element-plus';

// 臨時(shí)允許多個(gè) Message
ElMessage({ message: '提示 1', grouping: true }); // grouping 可堆疊
ElMessage({ message: '提示 2', grouping: true });

// 恢復(fù)單例模式
ElMessage({ message: '唯一提示', max: 1 });

三、最佳實(shí)踐總結(jié)

方案

ElementUI 2.X

Element Plus 3.X

Notification 單例

max: 1

(全局配置)

max: 1

(全局配置)

Message 單例

手動(dòng)封裝單例

max: 1

(全局配置)

動(dòng)態(tài)控制

需手動(dòng)管理

支持 grouping 參數(shù)

推薦方案:

  • ElementUI 2.X:Notification 用 max,Message 手動(dòng)封裝單例
  • Element Plus 3.X:直接全局配置 max: 1

四、常見問題

多個(gè) Message 如何排隊(duì)顯示?

可以封裝 消息隊(duì)列,如:

const messageQueue = [];
let isShowing = false;

function showNextMessage() {
if (messageQueue.length === 0 || isShowing) return;
  isShowing = true;
const options = messageQueue.shift();
  Message({
    ...options,
    onClose: () => {
      isShowing = false;
      options.onClose?.();
      showNextMessage();
    }
  });
}

exportfunction queueMessage(options) {
  messageQueue.push(options);
  showNextMessage();
}
責(zé)任編輯:趙寧寧 來源: 前端歷險(xiǎn)記
相關(guān)推薦

2017-10-24 15:11:39

Python 2.x 3.x

2011-04-27 09:39:53

EclipseIntelliJ

2015-09-14 10:18:37

2009-04-20 16:15:59

2014-11-28 09:47:26

Python

2012-06-17 20:19:29

2024-11-05 09:25:45

2009-10-24 23:24:46

ibmSystem x365虛擬化

2017-06-08 11:00:09

HDFSHadoopYARN

2011-08-16 10:41:40

安裝XcodeLion

2022-03-23 23:08:59

銳捷

2021-12-09 10:17:25

部署實(shí)戰(zhàn)Linux

2022-02-22 08:30:12

Husky代碼工作流

2024-07-01 08:18:14

2010-12-21 17:38:12

2009-10-24 23:20:49

System x365虛擬化

2022-03-18 09:00:00

開發(fā)Web服務(wù)應(yīng)用程序

2021-03-04 10:11:50

MongoDBSpring BootSpring Boot

2012-06-27 15:59:13

Python

2013-09-16 10:31:46

IBM System X86System x
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)