详细分析Element Plus中的ElMessageBox弹窗用法(附Demo及模版)

news/2024/6/16 21:48:38

目录

  • 前言
  • 1. 基本知识
  • 2. Demo
  • 3. 实战
  • 4. 模版

前言

由于需要在登录时,附上一些用户说明书的弹窗
对于ElMessageBox的基本知识详细了解
可通过官网了解基本的语法知识ElMessageBox官网基本知识

1. 基本知识

Element Plus 是一个基于 Vue 3 的组件库,其中包括各种类型的弹窗组件,用于在网页上显示信息或与用户进行交互

主要的弹窗组件包括 ElMessageBox.alert、ElMessageBox.prompt 和 ElMessageBox.confirm 等

  • ElMessageBox.prompt
    用于显示带有输入框的对话框
    用于需要用户输入信息的场景
import { ElMessageBox } from 'element-plus'ElMessageBox.prompt('请输入你的邮箱','提示',{confirmButtonText: '确定',cancelButtonText: '取消',}
).then(({ value }) => {console.log('用户输入的邮箱:', value)
}).catch(() => {console.log('取消输入')
})
  • ElMessageBox.alert
    用于显示带有确认按钮的对话框
    用于告知用户某些信息
import { ElMessageBox } from 'element-plus'ElMessageBox.alert('这是一段内容','标题',{confirmButtonText: '确定',callback: action => {console.log(action)}}
)
  • ElMessageBox.confirm
    用于显示带有确认和取消按钮的对话框
    用于需要用户确认或取消某些操作的场景
import { ElMessageBox } from 'element-plus'ElMessageBox.confirm('此操作将永久删除该文件, 是否继续?','提示',{confirmButtonText: '确定',cancelButtonText: '取消',type: 'warning',}
).then(() => {console.log('确认')
}).catch(() => {console.log('取消')
})

对于上述基本参数

参数描述
title对话框的标题
message对话框的消息内容,可以是字符串或 HTML
type消息类型,如 success, info, warning, error
iconClass自定义图标的类名
customClass对话框自定义类名
showClose是否显示右上角关闭按钮,默认为 true
closeOnClickModal是否可以通过点击遮罩层关闭对话框,默认为 true
closeOnPressEscape是否可以通过按下 Esc 键关闭对话框,默认为 true
showCancelButton是否显示取消按钮,默认为 false
cancelButtonText取消按钮的文本内容
confirmButtonText确认按钮的文本内容
cancelButtonClass自定义取消按钮的类名
confirmButtonClass自定义确认按钮的类名
beforeClose关闭前的回调函数,可以用于阻止对话框的关闭
callback对话框关闭时的回调函数
inputPlaceholder输入框的占位符(仅用于 prompt)
inputValue输入框的初始值(仅用于 prompt)
inputType输入框的类型(仅用于 prompt)
inputPattern输入框的校验正则表达式(仅用于 prompt)
inputValidator输入框的校验函数(仅用于 prompt)
inputErrorMessage输入框校验失败时的错误提示(仅用于 prompt)

2. Demo

对应的Demo示例:

  • ElMessageBox.prompt
import { ElMessageBox } from 'element-plus'const showPrompt = () => {ElMessageBox.prompt('请输入你的名字','输入框',{confirmButtonText: '确定',cancelButtonText: '取消',inputPlaceholder: '名字',showClose: false,closeOnClickModal: false,closeOnPressEscape: false,}).then(({ value }) => {console.log('输入的名字:', value)}).catch(() => {console.log('已取消')})
}
  • ElMessageBox.alert
import { ElMessageBox } from 'element-plus'const showAlert = () => {ElMessageBox.alert('操作成功','提示',{confirmButtonText: '确定',type: 'success',showClose: false,closeOnClickModal: false,closeOnPressEscape: false,}).then(() => {console.log('已确认')})
}
  • ElMessageBox.confirm
import { ElMessageBox } from 'element-plus'const showConfirm = () => {ElMessageBox.confirm('是否确认删除此项?','删除确认',{confirmButtonText: '确认',cancelButtonText: '取消',type: 'warning',showClose: false,closeOnClickModal: false,closeOnPressEscape: false,beforeClose: (action, instance, done) => {if (action === 'confirm') {// 执行一些操作done()} else {done()}}}).then(() => {console.log('已确认')}).catch(() => {console.log('已取消')})
}

如果需要自定义的样式,可通过如下:

ElMessageBox.confirm('内容','标题',{customClass: 'my-custom-class',confirmButtonText: '确认',cancelButtonText: '取消',}
)

css如下:

.my-custom-class .el-message-box__btns {justify-content: center;
}
.my-custom-class .el-button--primary {width: 100%;margin: 0;
}

3. 实战

const handleLogin = async (params) => {loginLoading.value = truetry {await getTenantId()const data = await validForm()if (!data) {return}loginData.loginForm.captchaVerification = params.captchaVerificationconst res = await LoginApi.login(loginData.loginForm)if (!res) {return}loading.value = ElLoading.service({lock: true,text: '正在加载系统中...',background: 'rgba(0, 0, 0, 0.7)'})if (loginData.loginForm.rememberMe) {authUtil.setLoginForm(loginData.loginForm)} else {authUtil.removeLoginForm()}authUtil.setToken(res)if (!redirect.value) {redirect.value = '/'}// 判断是否为SSO登录if (redirect.value.indexOf('sso') !== -1) {window.location.href = window.location.href.replace('/login?redirect=', '')} else {push({ path: redirect.value || permissionStore.addRouters[0].path })}} finally {loginLoading.value = falseloading.value.close()// 登录成功后显示弹窗提示await ElMessageBox.confirm(`<div><p>尊敬的客户:<br><br>您好!xxxx前认真阅读以下须知:<br><br>1xxxxxx<br><br><input type="checkbox" id="agree-checkbox" /> <label for="agree-checkbox">我已认真阅读并知悉以上须知</label></p></div>`,'xxx须知', {confirmButtonText: '同意',showClose: false, // 禁止关闭showCancelButton: false, // 隐藏右上角的关闭按钮closeOnClickModal: false, // 禁止点击遮罩层关闭closeOnPressEscape: false, // 禁止按下 Esc 键关闭dangerouslyUseHTMLString: true,customClass: 'full-width-button', // 添加自定义类名beforeClose: (action, instance, done) => {if (action === 'confirm' && !document.getElementById('agree-checkbox').checked) {instance.confirmButtonLoading = falsereturn ElMessageBox.alert('请勾选“我已认真阅读并知悉以上须知”以继续', '提示', {confirmButtonText: '确定',type: 'warning'})}done()}});}
}// 添加样式以使按钮占据整个宽度
const style = document.createElement('style');
style.type = 'text/css';
style.innerHTML = `.el-message-box.full-width-button .el-message-box__btns {display: flex;justify-content: center;}.el-message-box.full-width-button .el-button--primary {width: 100%;margin: 0;}
`;
document.head.appendChild(style);

总体截图如下:

在这里插入图片描述

下半部分的截图如下:

在这里插入图片描述

4. 模版

针对上述应用的需求,可以附实战的Demo

import { ElMessage, ElMessageBox, ElNotification } from 'element-plus'
import { useI18n } from './useI18n'
export const useMessage = () => {const { t } = useI18n()return {// 消息提示info(content: string) {ElMessage.info(content)},// 错误消息error(content: string) {ElMessage.error(content)},// 成功消息success(content: string) {ElMessage.success(content)},// 警告消息warning(content: string) {ElMessage.warning(content)},// 弹出提示alert(content: string) {ElMessageBox.alert(content, t('common.confirmTitle'))},// 错误提示alertError(content: string) {ElMessageBox.alert(content, t('common.confirmTitle'), { type: 'error' })},// 成功提示alertSuccess(content: string) {ElMessageBox.alert(content, t('common.confirmTitle'), { type: 'success' })},// 警告提示alertWarning(content: string) {ElMessageBox.alert(content, t('common.confirmTitle'), { type: 'warning' })},// 通知提示notify(content: string) {ElNotification.info(content)},// 错误通知notifyError(content: string) {ElNotification.error(content)},// 成功通知notifySuccess(content: string) {ElNotification.success(content)},// 警告通知notifyWarning(content: string) {ElNotification.warning(content)},// 确认窗体confirm(content: string, tip?: string) {return ElMessageBox.confirm(content, tip ? tip : t('common.confirmTitle'), {confirmButtonText: t('common.ok'),cancelButtonText: t('common.cancel'),type: 'warning'})},// 删除窗体delConfirm(content?: string, tip?: string) {return ElMessageBox.confirm(content ? content : t('common.delMessage'),tip ? tip : t('common.confirmTitle'),{confirmButtonText: t('common.ok'),cancelButtonText: t('common.cancel'),type: 'warning'})},// 导出窗体exportConfirm(content?: string, tip?: string) {return ElMessageBox.confirm(content ? content : t('common.exportMessage'),tip ? tip : t('common.confirmTitle'),{confirmButtonText: t('common.ok'),cancelButtonText: t('common.cancel'),type: 'warning'})},// 提交内容prompt(content: string, tip: string) {return ElMessageBox.prompt(content, tip, {confirmButtonText: t('common.ok'),cancelButtonText: t('common.cancel'),type: 'warning'})}}
}

http://www.mrgr.cn/p/25176307

相关文章

java文档管理系统的设计与实现源码(springboot+vue+mysql)

风定落花生&#xff0c;歌声逐流水&#xff0c;大家好我是风歌&#xff0c;混迹在java圈的辛苦码农。今天要和大家聊的是一款基于springboot的文档管理系统的设计与实现。项目源码以及部署相关请联系风歌&#xff0c;文末附上联系信息 。 项目简介&#xff1a; 文档管理系统的…

C++容器之映射(std::map)

目录 1 概述2 使用实例3 接口使用3.1 construct3.2 assigns3.3 iterators3.4 capacity3.5 access3.6 insert3.7 erase3.8 swap3.9 clear3.10 emplace3.11 emplace_hint3.12 key_comp3.13 value_comp3.14 find/count3.15 upper_bound/upper_bound/equal_range3.16 get_allocator…

TALLRec论文阅读笔记

TALLRec: An Effective and Efficient Tuning Framework to Align Large Language Model with Recommendation论文阅读笔记 Abstract 存在的问题: ​ 由于LLM的训练任务和推荐任务之间存在显著差异,以及训练前的推荐数据不足,LLM在推荐任务中的表现仍然不理想。 解决方案: …

NumPy 正态分布与 Seaborn 可视化指南

正态分布(高斯分布)是重要的概率模型,具有钟形曲线特征,由均值μ和标准差σ描述。NumPy的`random.normal()`可生成正态分布随机数,Seaborn库方便绘制分布图。正态分布广泛应用于统计学、机器学习、金融和工程等领域。练习包括生成正态分布数据、比较不同标准差影响及模拟考…

使用本地大语言模型和Langchain手搓免费的AI搜索问答助手

1 概述 大语言模型虽然已经有了很多的背景知识,但针对模型训练之后新产生的内容,或者领域内的知识进行提问,大模型本身通常无法准确给出回应,一个常用的解决方法是,借助检索增强生成(RAG),将能够用于回答问题的相关上下文给到大模型,利用大模型强大的理解和生成能力,…

免费,Python蓝桥杯等级考试真题--第6级(含答案解析和代码)

Python蓝桥杯等级考试真题–第6级 一、 选择题 答案&#xff1a;D 解析&#xff1a;4411*4&#xff0c;超出范围&#xff0c;故答案为D。 答案&#xff1a;B 解析&#xff1a;5<8<10&#xff0c;故答案为B。 答案&#xff1a;A 解析&#xff1a;先比较a&#xff0c;然后…

Further Generalizations of the Jaccard Index

目录概Jaccard Index推广到 multisets推广到 Multiple setsCosta L. Further generalizations of the jaccard index. 2021.概 本文介绍了 Jaccard Index (Jaccard Similarity), 和它的一些变种. Jaccard Index对于两个普通的集合 \(A, B\), 它们的 Jaccard Index 为 \[J(A, B…

deepseek是哪家公司

deepblue是什么公司 DeepSeek是杭州深度求索人工智能基础技术研究有限公司的简称。12 杭州深度求索人工智能基础技术研究有限公司&#xff0c;成立于2023年&#xff0c;位于浙江省杭州市&#xff0c;是一家专注于研究和试验发展的企业。该公司的注册资本为1000万人民币&…

终端安全管理系统、天锐DLP(数据泄露防护系统)| 数据透明加密保护,防止外泄!

终端作为企业员工日常办公、数据处理和信息交流的关键工具&#xff0c;承载着企业运营的核心信息资产。一旦终端安全受到威胁&#xff0c;企业的敏感数据将面临泄露风险&#xff0c;业务流程可能遭受中断&#xff0c;甚至整个企业的运营稳定性都会受到严重影响。 因此&#xff…

无网环境禁止 WPS 提示登录,且基本功能按钮可用

目前 WPS 升级后&#xff0c;每次打开都会提示你登录 WPS&#xff0c;并且在未登录之前所有基本功能按钮是置灰状态&#xff0c;无法使用。 如此一来&#xff0c;在内网或无网环境&#xff0c;我们无法登陆 WPS &#xff0c;就给我们的使用带来了极大的不便&#xff0c;那么有没…

院图片库兼容性修复

图片库问题处理 原工程文件使用node-sass,而新版node.js已经弃用,应卸载node-sass,重新安装dart-sass 1.卸载node-sass npm uninstall node-sass2.安装dart-sass npm install sass sass-loader -D3.安装所有包 npm install4.修改element-plus配置 https://zhuanlan.zhihu.co…

rbenv:Ruby 多版本管理利器

在 Ruby 开发的世界中,经常需要面对不同项目使用不同 Ruby 版本的情况。这时,一个高效、灵活且易于使用的 Ruby 版本管理工具就显得尤为重要。 rbenv 正是这样一个工具,它允许开发者在同一台计算机上轻松安装、切换和管理多个 Ruby 版本。本文将详细介绍 rbenv 的安装、基本…

GPT搜索引擎原型曝光!

OpenAI发布会前一天&#xff0c;员工集体发疯中……上演大型套娃行为艺术。 A&#xff1a;我为B的兴奋感到兴奋&#xff1b;B&#xff1a;我为C的兴奋感到兴奋……Z&#xff1a;我为这些升级感到兴奋 与此同时还有小动作不断&#xff0c;比如现在GPT-4的文字描述已不再是“最先…

海山数据库(He3DB)代理ProxySQL使用详解:(二)功能实测

读写分离实测 ProxySQL官方demo演示了三种读写分离的方式&#xff1a;使用不同的端口进行读写分离、使用正则表达式进行通用的读写分离、使用正则和digest进行更智能的读写分离。最后一种是针对特定业务进行的优化调整&#xff0c;也可将其归结为第二种方式&#xff0c;下边分…

Docker(四) 文件和网络

1 Dockerfile 1.1 什么是Dockerfile Dockerfile是一个文本文件&#xff0c;包含一系列命令&#xff0c;这些命令用于在 Docker 镜像中自动执行操作。Dockerfile 定义了如何构建 Docker 镜像的步骤和所需的操作。 Dockerfile 中包含的命令可以设置和定制容器的环境&#xff0c;…

Viso的对象图形复制到word,发现图形画布底部有大量空白,如何解决

1 viso对象插入到word VIso的图可以作为对象插入到word中,直接复制即可,复制后,可以在word中双击,关联到viso中,进行更改,很方便。 正常情况下,在viso中做好图形后,直接复制到word中即可,在word中双击,关联到viso中。如下图:偶尔会存在一些格式比例大小的问题,导致…

马蹄集 oj赛(双周赛第二十七次)

目录 栈的min 外卖递送 奇偶序列 sort 五彩斑斓的世界 括号家族 名次并列 栈间 双端队列 合并货物 逆序对 活动分组 栈的min 难度:黄金巴 占用内存:128 M时间限制:1秒 小码哥又被安排任务了&#xff0c;这次他需要要设计一个堆栈&#xff0c;他除了可以满足正常的栈…

变频器通过Modbus转Profinet网关连接电机与PLC通讯

Modbus转Profinet网关(XD-MDPN100)是一种能够实现Modbus协议和Profinet协议之间转换的设备。Modbus转Profinet网关可提供单个或多个RS485接口,PLC作为控制中枢,变频器作为控制电机转速,通过Modbus转Profinet网关,实现对电机的远程监控和调节,使得生产过程更加智能化和精…

Python筑基之旅-MySQL数据库(一)

目录 一、MySQL数据库 1、简介 2、优点 2-1、开源和免费 2-2、高性能 2-3、可扩展性 2-4、易用性 2-5、灵活性 2-6、安全性和稳定性 2-7、丰富的功能 2-8、结合其他工具和服务 2-9、良好的兼容性和移植性 3、缺点 3-1、对大数据的支持有限 3-2、缺乏全文…