Message.js 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. import { ElMessage, ElNotification } from 'element-plus'
  2. // 存储当前活动的消息或通知实例
  3. let activeInstance = null
  4. export const Message = () => {
  5. // 显示弹框的通用方法
  6. const show = (type, cover, content, showFn) => {
  7. // 如果有活动实例,先关闭它
  8. if (activeInstance && cover) {
  9. activeInstance.close()
  10. }
  11. // 显示新的弹框
  12. activeInstance = showFn(content)
  13. activeInstance.onClose = () => {
  14. activeInstance = null
  15. }
  16. }
  17. return {
  18. // 消息提示
  19. info(content, cover = false) {
  20. show('info', cover, content, (msg) => ElMessage.info(msg))
  21. },
  22. // 错误消息
  23. error(content, cover = false) {
  24. show('error', cover, content, (msg) => ElMessage.error(msg))
  25. },
  26. // 成功消息
  27. success(content, cover = false) {
  28. show('success', cover, content, (msg) => ElMessage.success(msg))
  29. },
  30. // 警告消息
  31. warning(content, cover = false) {
  32. show('warning', cover, content, (msg) => ElMessage.warning(msg))
  33. },
  34. // 通知提示
  35. notify(content, cover = false) {
  36. show('notify', cover, content, (msg) => ElNotification.info(msg))
  37. },
  38. // 错误通知
  39. notifyError(content, cover = false) {
  40. show('notifyError', cover, content, (msg) => ElNotification.error(msg))
  41. },
  42. // 成功通知
  43. notifySuccess(content, cover = false) {
  44. show('notifySuccess', cover, content, (msg) => ElNotification.success(msg))
  45. },
  46. // 警告通知
  47. notifyWarning(content, cover = false) {
  48. show('notifyWarning', cover, content, (msg) => ElNotification({
  49. title: '系统提示',
  50. message: msg,
  51. type: 'warning',
  52. }))
  53. },
  54. }
  55. }