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

前言

详细知识推荐阅读:详细分析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>

实战中类似的截图如下:

在这里插入图片描述

  • 19
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Element UI `MessageBox` 的 `inputValidator` 校验函数是在用户输入时触发的,而不是在失去焦点时触发的。如果您想要在失去焦点时触发校验函数,可以在 `MessageBox` 使用 `inputAttrs` 属性来设置输入框的属性,然后监听输入框的 `blur` 事件来触发校验函数。 下面是示例代码: ```javascript this.$msgbox({ title: '请输入用户名', message: '请输入您的用户名:', inputAttrs: { type: 'text', blur: this.validateInput // 监听输入框的 blur 事件,并触发校验函数 }, showCancelButton: true, confirmButtonText: '确定', cancelButtonText: '取消', inputValidator: (value) => { // 校验函数 if (!value) { return '用户名不能为空!'; } } }).then((value) => { console.log('输入的用户名是:' + value); }).catch(() => { console.log('取消输入用户名!'); }); ``` 在上面的代码,我们在 `inputAttrs` 设置了 `blur` 属性,并将其绑定到 `validateInput` 方法上。然后,在 `inputValidator` 定义了校验函数,如果用户名为空,则返回校验失败信息。 当用户在输入框输入完用户名后,移开焦点时,`validateInput` 方法会被触发,从而触发校验函数。如果校验失败,则会弹出提示框,要求用户重新输入。如果校验成功,则会执行 `then` 的方法,获取用户输入的用户名。如果用户取消输入,则会执行 `catch` 的方法,提示用户已取消输入。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

码农研究僧

你的鼓励将是我创作的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值