详细分析Element中的MessageBox基本知识(附Demo)

news2024/9/21 6:36:43

目录

  • 前言
  • 1. 基本知识
  • 2. Demo
    • 2.1 确认框
    • 2.2 警告框
    • 2.3 对话框
  • 3. this.$confirm

前言

详细知识推荐阅读:详细分析Element Plus中的ElMessageBox弹窗用法(附Demo及模版)

MessageBox则常用于Vue2

1. 基本知识

MessageBox 是 Element UI 提供的一个全局方法,用于创建各种对话框,如消息提示、确认框和输入框

MessageBox 可以通过引入 MessageBox 组件来使用,也可以通过全局挂载的方式使用 this.$confirm 等快捷方法

常用的方法如下:

  • MessageBox.alert(message, title, options):显示一个消息提示框
  • MessageBox.confirm(message, title, options):显示一个确认对话框
  • MessageBox.prompt(message, title, options):显示一个输入框

具体的参数说明如下:

  • message:对话框的内容,可以是字符串或 HTML 片段
  • title:对话框的标题
  • options:配置对象,用于定制对话框的行为和样式,包括以下常用选项:
    confirmButtonText:确认按钮的文本
    cancelButtonText:取消按钮的文本
    type:消息类型(success, warning, info, error)
    callback:按钮点击后的回调函数
    dangerouslyUseHTMLString:是否将 message 作为 HTML 片段

对应的返回值如下:返回一个 Promise,点击确认按钮会 resolve,点击取消按钮会 reject

2. Demo

2.1 确认框

<template>
  <div>
    <el-button @click="handleDelete(1)">Delete Item 1</el-button>
    <el-table :data="list">
      <el-table-column prop="name" label="Name"></el-table-column>
      <el-table-column label="Actions">
        <template slot-scope="scope">
          <el-button @click="handleDelete(scope.row.id)" type="danger" size="small">Delete</el-button>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script>
import { MessageBox, Message } from 'element-ui';

export default {
  data() {
    return {
      list: [
        { id: 1, name: 'Item 1' },
        { id: 2, name: 'Item 2' },
        { id: 3, name: 'Item 3' },
      ],
    };
  },
  methods: {
    handleDelete(id) {
      MessageBox.confirm('Are you sure you want to delete this item?', 'Warning', {
        confirmButtonText: 'Yes',
        cancelButtonText: 'No',
        type: 'warning',
      }).then(() => {
        // Simulate an API call to delete the item
        this.list = this.list.filter(item => item.id !== id);
        Message({
          type: 'success',
          message: 'Item deleted successfully!',
        });
      }).catch(() => {
        Message({
          type: 'info',
          message: 'Deletion cancelled',
        });
      });
    },
  },
};
</script>

<style>
@import '~element-ui/lib/theme-chalk/index.css';
</style>

2.2 警告框

<template>
  <div>
    <el-button @click="showAlert">Show Alert</el-button>
  </div>
</template>

<script>
import { MessageBox } from 'element-ui';

export default {
  methods: {
    showAlert() {
      MessageBox.alert('This is a warning message', 'Warning', {
        confirmButtonText: 'OK',
        type: 'warning',
      });
    },
  },
};
</script>

<style>
@import '~element-ui/lib/theme-chalk/index.css';
</style>

2.3 对话框

<template>
  <div>
    <el-button @click="showPrompt">Show Prompt</el-button>
  </div>
</template>

<script>
import { MessageBox } from 'element-ui';

export default {
  methods: {
    showPrompt() {
      MessageBox.prompt('Please input your name', 'Prompt', {
        confirmButtonText: 'OK',
        cancelButtonText: 'Cancel',
      }).then(({ value }) => {
        this.$message({
          type: 'success',
          message: 'Your name is: ' + value,
        });
      }).catch(() => {
        this.$message({
          type: 'info',
          message: 'Input cancelled',
        });
      });
    },
  },
};
</script>

<style>
@import '~element-ui/lib/theme-chalk/index.css';
</style>

3. this.$confirm

在 Vue 2 中使用 Element UI 时,可以通过全局方法 this.$confirm 等快捷方式来调用这些对话框,以简化代码并提升开发效率

实际上它是 MessageBox.confirm 的一个封装

具体的Demo如下:

<template>
  <div>
    <el-button @click="handleDelete(1)">Delete Item 1</el-button>
    <el-table :data="list">
      <el-table-column prop="name" label="Name"></el-table-column>
      <el-table-column label="Actions">
        <template slot-scope="scope">
          <el-button @click="handleDelete(scope.row.id)" type="danger" size="small">Delete</el-button>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script>
export default {
  data() {
    return {
      list: [
        { id: 1, name: 'Item 1' },
        { id: 2, name: 'Item 2' },
        { id: 3, name: 'Item 3' },
      ],
    };
  },
  methods: {
    handleDelete(id) {
      this.$confirm('Are you sure you want to delete this item?', 'Warning', {
        confirmButtonText: 'Yes',
        cancelButtonText: 'No',
        type: 'warning',
      }).then(() => {
        // Simulate an API call to delete the item
        this.list = this.list.filter(item => item.id !== id);
        this.$message({
          type: 'success',
          message: 'Item deleted successfully!',
        });
      }).catch(() => {
        this.$message({
          type: 'info',
          message: 'Deletion cancelled',
        });
      });
    },
  },
};
</script>

<style>
@import '~element-ui/lib/theme-chalk/index.css';
</style>

同步对比其差异:

<template>
  <div>
    <el-button @click="handleDelete(1)">Delete Item 1</el-button>
    <el-table :data="list">
      <el-table-column prop="name" label="Name"></el-table-column>
      <el-table-column label="Actions">
        <template slot-scope="scope">
          <el-button @click="handleDelete(scope.row.id)" type="danger" size="small">Delete</el-button>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script>
import { MessageBox, Message } from 'element-ui';

export default {
  data() {
    return {
      list: [
        { id: 1, name: 'Item 1' },
        { id: 2, name: 'Item 2' },
        { id: 3, name: 'Item 3' },
      ],
    };
  },
  methods: {
    handleDelete(id) {
      MessageBox.confirm('Are you sure you want to delete this item?', 'Warning', {
        confirmButtonText: 'Yes',
        cancelButtonText: 'No',
        type: 'warning',
      }).then(() => {
        // Simulate an API call to delete the item
        this.list = this.list.filter(item => item.id !== id);
        Message({
          type: 'success',
          message: 'Item deleted successfully!',
        });
      }).catch(() => {
        Message({
          type: 'info',
          message: 'Deletion cancelled',
        });
      });
    },
  },
};
</script>

<style>
@import '~element-ui/lib/theme-chalk/index.css';
</style>

实战中类似的截图如下:

在这里插入图片描述

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1696557.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

《欢乐钓鱼大师》辅助:新手钓鱼全新攻略大全!

《欢乐钓鱼大师》是一款充满趣味和挑战的钓鱼游戏。在游戏中&#xff0c;玩家不仅可以体验钓鱼的乐趣&#xff0c;还可以通过不同的钓鱼竿和鱼卡来提升自己的钓鱼技能。为了帮助新手和老玩家更好地体验游戏&#xff0c;本文将为您提供详细的游戏攻略。 1. 游戏目标 在《欢乐钓…

window环境下QT5开发环境的搭建

1、安装visual Stusio 15 生成工具2012 2、安装Visual studio Enterprise 2017 3、Visual studio Enterprise 2017安装完成之后&#xff0c; 修改&#xff1a;选择桌面调试&#xff0c;如下&#xff1a; 4、打开QTcreator&#xff0c;选项中&#xff0c;配置编译器&#xff…

详细分析tcping的基本知识以及用法

目录 前言1. 安装配置2. 基本知识3. Demo 前言 针对ping的基本知识推荐阅读&#xff1a;详细分析ping的基本知识以及常见网络故障的诊断&#xff08;图文解析&#xff09; 1. 安装配置 针对Window的下载如下&#xff1a; 安装路径&#xff1a;tcping官网 下载tcping.exe&a…

2024最新 Jenkins + Docker实战教程(一) - Jenkins介绍及安装

&#x1f604; 19年之后由于某些原因断更了三年&#xff0c;23年重新扬帆起航&#xff0c;推出更多优质博文&#xff0c;希望大家多多支持&#xff5e; &#x1f337; 古之立大事者&#xff0c;不惟有超世之才&#xff0c;亦必有坚忍不拔之志 &#x1f390; 个人CSND主页——Mi…

pytorch笔记:torch.nn.Flatten()

1 介绍 torch.nn.Flatten(start_dim1, end_dim-1) 将一个连续的维度范围扁平化为一个张量 start_dim (int)要开始扁平化的第一个维度&#xff08;默认值 1&#xff09;end_dim (int)要结束扁平化的最后一个维度&#xff08;默认值 -1&#xff09; 2 举例 input torch.ra…

PyTorch基础(22)-- torch.chunk()方法

分享一下自己目前在维护的Github项目&#xff0c;由于本人博士阶段接触了一个全新的研究方向-----使用机器学习、强化学习、深度学习等方法解决组合优化问题&#xff0c;维护这个项目的目的&#xff1a; &#xff08;1&#xff09;记录自己阅读过的paper&#xff0c;同时分享一…

Attention相关问题笔试解析。

Attention相关问题笔试解析。 题目描述一&#xff1a;【选择】题目描述二&#xff1a;【简答】题目描述三&#xff1a;【代码】Scaled Dot-Product Attention&#xff1a;下面是用PyTorch实现的一个Attention机制的代码。这个实现包括一个简单的Scaled Dot-Product Attention机…

多模态交互式 AI 代理的兴起:探索 Google 的 Astra 和 OpenAI 的 ChatGPT-4o应用

OpenAI的发展 聊天GPT-4o 和 谷歌的阿斯特拉 标志着交互式人工智能代理的新阶段&#xff1a;多模式交互式人工智能代理的兴起。这次旅程开始于 Siri 和 Alexa的&#xff0c;它将语音激活的人工智能带入主流用途&#xff0c;并通过语音命令改变了我们与技术的交互。尽管有影响&a…

Rolla‘s homework:Image Processing with Python Final Project

对比学习Yolo 和 faster rcnn 两种目标检测 要求 Image Processing with Python Final Project Derek TanLoad several useful packages that are used in this notebook:Image Processing with Python Final Project Project Goals: • Gain an understanding of the object …

【python】python小区住房数据可视化(源码+数据)【独一无二】

&#x1f449;博__主&#x1f448;&#xff1a;米码收割机 &#x1f449;技__能&#x1f448;&#xff1a;C/Python语言 &#x1f449;公众号&#x1f448;&#xff1a;测试开发自动化【获取源码商业合作】 &#x1f449;荣__誉&#x1f448;&#xff1a;阿里云博客专家博主、5…

树莓派4B 有电但无法启动

试过多个SD卡&#xff0c;反复烧系统镜像都无法启动。接HDMI显示器没有信号输出&#xff0c;上电后PWR红灯长亮&#xff0c;ACT绿灯闪一下就不亮了&#xff0c;GPIO几个电源脚有电&#xff0c;芯片会发热&#xff0c;测量多个TP点电压好像都正常。 ……

【电子元件】TL431 电压基准

TL431(C23892)是一种常用的可调节精密电压基准和电压调节器。它广泛应用于电源管理、精密参考电压和稳压电路等领域。以下是TL431的一些关键特点和使用方法&#xff1a; 关键特点 可调输出电压&#xff1a;TL431的输出电压可以通过外部电阻网络在2.495V到36V范围内调整。精度高…

【笔记】Pytorch安装配置

参考视频 安装前建议预留至少10个G的空间&#xff0c;会省下很多麻烦 查看安装是否成功&#xff0c;可以在Anaconda Prompt里输入conda list查看conda环境是否配置了pytorch/torchvision 1.安装anaconda 2.安装 CUDA CUDA在官网直接安装即可&#xff0c;需要先查看自己电脑…

fpga问题整理

1、quartus联合modelsim仿真 无波形 问题&#xff1a; modelsim仿真无波形&#xff0c;打开transcript可以看到警告。 警告&#xff1a; # ** Warning: (vlog-2083) f:/program files/altera/ 13.1/quartus/ eda/sim_lib/ altera_lnsim.sv(22728): Carriage return (0x0D) is…

【前端笔记】Vue项目报错Error: Cannot find module ‘webpack/lib/RuleSet‘

网上搜了下发现原因不止一种&#xff0c;这里仅记录本人遇到的原因和解决办法&#xff0c;仅供参考 原因&#xff1a;因为某种原因导致本地package.json中vue/cli与全局vue/cli版本不同导致冲突。再次提示&#xff0c;这是本人遇到的&#xff0c;可能和大家有所不同&#xff0c…

子线程无法访问父线程中通过ThreadLocal设置的变量

学习过ThreadLocal的童鞋都知道&#xff0c;在子线程中&#xff0c;是无法访问父线程通过ThreadLocal设置的变量的。 package thread;/*** author heyunlin* version 1.0*/ public class ThreadLocalExample {public static void main(String[] args) throws InterruptedExcep…

搭建CMS系统

搭建CMS系统 1 介绍 内容管理系统&#xff08;Content Management System&#xff0c;CMS&#xff09;是一种用于管理、发布和修改网站内容的系统。开源的CMS系统有WordPress、帝国CMS等&#xff0c;国产的Halo很不错。 WordPress参考地址 # 官网 https://wordpress.org/# …

信号量——多线程

信号量的本质就是一个计数器 在多线程访问临界资源的时候&#xff0c;如果临界资源中又有很多份分好的资源&#xff0c;那么就可以通过信号量来表示里面还有多少份资源&#xff0c;且每份资源只有一个线程可以访问 线程申请信号量成功&#xff0c;就一定有一份资源是你的&…