当前位置: 首页 > news >正文

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

目录

  • 前言
  • 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'})}}
}

相关文章:

  • Java 三种主流的消息中间件 RabbitMQ、Kafka 和 RocketMQ 特点以及适用,使用场景 学习总结
  • 【设计模式】JAVA Design Patterns——Command(事务模式)
  • MySQL视图教程(01):创建视图
  • YOLOv10 论文学习
  • 一.架构设计
  • 互联网十万个为什么之什么是虚拟化?
  • kubenetes中K8S的命名空间状态异常强制删除Terminating的ns
  • 架构师必考题--软件系统质量属性
  • 【蓝桥杯】国赛普及-
  • 变分自动编码器(VAE)深入理解与总结
  • ffplay 使用文档介绍
  • js 生成二维码
  • Spring Boot:SpringBoot 如何优雅地定制JSON响应数据返回
  • 民国漫画杂志《时代漫画》第16期.PDF
  • 虹科Pico汽车示波器 | 免拆诊断案例 | 2012 款雪佛兰科鲁兹车偶尔多个故障灯异常点亮
  • 【译】JS基础算法脚本:字符串结尾
  • “寒冬”下的金三银四跳槽季来了,帮你客观分析一下局面
  • 【391天】每日项目总结系列128(2018.03.03)
  • 【MySQL经典案例分析】 Waiting for table metadata lock
  • idea + plantuml 画流程图
  • Java深入 - 深入理解Java集合
  • jdbc就是这么简单
  • MySQL几个简单SQL的优化
  • Mysql数据库的条件查询语句
  • React 快速上手 - 07 前端路由 react-router
  • vue-router的history模式发布配置
  • vue总结
  • 第13期 DApp 榜单 :来,吃我这波安利
  • 短视频宝贝=慢?阿里巴巴工程师这样秒开短视频
  • 构造函数(constructor)与原型链(prototype)关系
  • 使用Maven插件构建SpringBoot项目,生成Docker镜像push到DockerHub上
  • 推荐一个React的管理后台框架
  • 微服务入门【系列视频课程】
  • 学习笔记:对象,原型和继承(1)
  • Java性能优化之JVM GC(垃圾回收机制)
  • ​什么是bug?bug的源头在哪里?
  • #微信小程序:微信小程序常见的配置传旨
  • ()、[]、{}、(())、[[]]命令替换
  • (10)STL算法之搜索(二) 二分查找
  • (代码示例)使用setTimeout来延迟加载JS脚本文件
  • (附源码)springboot宠物管理系统 毕业设计 121654
  • (附源码)小程序 交通违法举报系统 毕业设计 242045
  • (黑马出品_高级篇_01)SpringCloud+RabbitMQ+Docker+Redis+搜索+分布式
  • (免费领源码)python#django#mysql校园校园宿舍管理系统84831-计算机毕业设计项目选题推荐
  • (原創) 如何動態建立二維陣列(多維陣列)? (.NET) (C#)
  • (转)shell中括号的特殊用法 linux if多条件判断
  • (转)一些感悟
  • .Net Core webapi RestFul 统一接口数据返回格式
  • .NET Reactor简单使用教程
  • .net wcf memory gates checking failed
  • .net安装_还在用第三方安装.NET?Win10自带.NET3.5安装
  • .NET大文件上传知识整理
  • .NET简谈互操作(五:基础知识之Dynamic平台调用)
  • /usr/bin/python: can't decompress data; zlib not available 的异常处理
  • ??myeclipse+tomcat