electron-vite封装UI级的消息提示

说明

Electron + Vite + Vue3 + Element Plus
Electron中写提示有两种方案:

  • 系统级:electron带的dialog相关API
  • UI级:UI框架内部的提示,如ElMessageElMessageBoxElNotification

今天来封装一下UI级别的提示

代码

效果图
源码

代码封装在hooks中,借鉴了若依:

javascript 复制代码
// src/hooks/useMessage.js
import { ElMessage, ElMessageBox, ElNotification, ElLoading } from 'element-plus'

let loadingInstance;

export const useMessage = () => {
  return {
    // 消息提示
    info(content) {
      ElMessage.info(content)
    },
    // 错误消息
    error(content) {
      ElMessage.error(content)
    },
    // 成功消息
    success(content) {
      ElMessage.success(content)
    },
    // 警告消息
    warning(content) {
      ElMessage.warning(content)
    },
    // 弹出提示
    alert(content) {
      ElMessageBox.alert(content, '系统提示')
    },
    // 错误提示
    alertError(content) {
      ElMessageBox.alert(content, '系统提示', { type: 'error' })
    },
    // 成功提示
    alertSuccess(content) {
      ElMessageBox.alert(content, '系统提示', { type: 'success' })
    },
    // 警告提示
    alertWarning(content) {
      ElMessageBox.alert(content, '系统提示', { type: 'warning' })
    },
    // 通知提示
    notify(content) {
      ElNotification.info(content)
    },
    // 错误通知
    notifyError(content) {
      ElNotification.error(content)
    },
    // 成功通知
    notifySuccess(content) {
      ElNotification.success(content)
    },
    // 警告通知
    notifyWarning(content) {
      ElNotification.warning(content)
    },
    // 确认窗体
    confirm(content, tip) {
      return ElMessageBox.confirm(content, tip ? tip : '系统提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
    },
    // 删除窗体
    delConfirm(content, tip) {
      return ElMessageBox.confirm(
        content ? content : '是否删除所选中数据?',
        tip ? tip : '系统提示',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }
      )
    },
    // 提交内容
    prompt(content, tip) {
      return ElMessageBox.prompt(content, tip, {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
    },

    // 打开遮罩层
    loading(content) {
      loadingInstance = ElLoading.service({
        lock: true,
        text: content,
        background: "rgba(0, 0, 0, 0.7)",
      })
    },

    // 关闭遮罩层
    closeLoading() {
      loadingInstance.close();
    }
  }
}
用法

vue组件中直接引用:

javascript 复制代码
import { useMessage } from "@/hooks/useMessage";
const message = useMessage()
message.confirm('马云想你转账500万,是否接收?')
相关推荐
鱼樱前端22 分钟前
今天介绍下最新更新的Vite7
前端·vue.js
coder_pig1 小时前
跟🤡杰哥一起学Flutter (三十四、玩转Flutter手势✋)
前端·flutter·harmonyos
万少1 小时前
01-自然壁纸实战教程-免费开放啦
前端
独立开阀者_FwtCoder1 小时前
【Augment】 Augment技巧之 Rewrite Prompt(重写提示) 有神奇的魔法
前端·javascript·github
yuki_uix1 小时前
AI辅助网页设计:从图片到代码的实践探索
前端
我想说一句1 小时前
事件机制与委托:从冒泡捕获到高效编程的奇妙之旅
前端·javascript
陈随易1 小时前
MoonBit助力前端开发,加密&性能两不误,斐波那契测试提高3-4倍
前端·后端·程序员
汤姆Tom1 小时前
JavaScript reduce()函数详解
javascript
小飞悟2 小时前
你以为 React 的事件很简单?错了,它暗藏玄机!
前端·javascript·面试
中微子2 小时前
JavaScript 事件机制:捕获、冒泡与事件委托详解
前端·javascript