# Message-box 二次确认框

模拟系统的消息提示框而实现的一套模态对话框组件,用于消息提示、确认消息和提交内容。如果需要弹出较为复杂的内容,请使用 Modal。

# 代码演示

基础用法

查看代码

<template>
  <w-button @click="open">点击打开 Message Box</w-button>
</template>

<script>
  export default {
    methods: {
      open() {
        this.$Confirm('此操作将永久删除该文件, 是否继续?', '提示', {
          type: 'warning',
          lockScroll: true,
          distinguishCancelAndClose: true
        })
          .then(() => {
            this.$Message({
              type: 'success',
              message: '删除成功!'
            })
          })
          .catch((action) => {
            console.log(action)
            this.$Message({
              message: '已取消删除'
            })
          })
      }
    }
  }
</script>

内容换行规则 👉 V2.4.29

通过属性 word-break 设置。

查看代码

<template>
  <w-button @click="open">点击打开 Message Box</w-button>
</template>

<script>
  export default {
    methods: {
      open() {
        this.$Confirm('保存追溯码异常,甲硝唑片的追溯码:30957011111111123452345345,在发药的追溯码中不存在,请检查!是否退药?', '提示', {
          type: 'warning',
          lockScroll: true,
          wordBreak: 'break-all'
        })
          .then(() => {
            this.$Message({
              type: 'success',
              message: '删除成功!'
            })
          })
          .catch((action) => {
            console.log(action)
            this.$Message({
              message: '已取消删除'
            })
          })
      }
    }
  }
</script>

可拖拽、🎉 推拽到屏幕外 V2.5.16

查看代码

<template>
  <w-button @click="open">点击打开 Message Box</w-button>
  <w-button @click="open2">拖拽到屏幕外</w-button>
</template>

<script>
  export default {
    methods: {
    open2() {
        this.$Confirm('此操作将永久删除该文件, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning',
          draggable: true,
          lockScroll: true,
          overflow: true
        })
          .then(() => {
            this.$Message({
              type: 'success',
              message: '删除成功!'
            })
          })
          .catch(() => {
            this.$Message({
              message: '已取消删除'
            })
          })
      },
      open() {
        this.$Confirm('此操作将永久删除该文件, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          draggable: true,
          type: 'warning'
        }).then(() => {
          this.$Message({
            type: 'success',
            message: '删除成功!'
          });
        }).catch(() => {
          this.$Message({
            message: '已取消删除'
          });
        });
      }
    }
  }
</script>

提交内容 👉 V2.4.7

调用$Prompt方法即可打开消息提示,它模拟了系统的 prompt。可以用 inputRows 设置文本域的行数,默认为1;可以用inputPattern字段自己规定匹配模式,或者用inputValidator规定校验函数,可以返回Boolean或String,返回false或字符串时均表示校验未通过,同时返回的字符串相当于定义了inputErrorMessage字段。此外,可以用inputPlaceholder字段来定义输入框的占位符。

查看代码

<template>
  <w-button @click="open">点击打开 Message Box</w-button>
</template>

<script>
  export default {
    methods: {
      open() {
        this.$Prompt('请输入手机号', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          inputPattern: /^1[3456789]d{9}$/,
          inputErrorMessage: '号码格式不正确'
        })
          .then(({ value }) => {
            this.$Message({
              type: 'success',
              message: '你的手机号是: ' + value
            })
          })
          .catch(() => {
            this.$Message({
              type: 'info',
              message: '取消输入'
            })
          })
      }
    }
  }
</script>

不同状态

用来显示「成功、警告、消息、错误」类的操作反馈。

查看代码

<template>
  <section>
      <w-button v-for="item in messageList"
        :type="item.type"
        :key="item.type"
        @click="messageShow(item)">{{item.label}}</w-button>
  </section>
</template>
<script>
  export default {
    data () {
      return {
        messageList: [
          {
            label: '消息',
            type: 'info'
          },
          {
            label: '成功',
            type: 'success'
          },
          {
            label: '警告',
            type: 'warning'
          },
          {
            label: '错误',
            type: 'error'
          }
        ]
      }
    },
    methods: {
      open (item) {
        this.$Confirm('此操作将永久删除该文件, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: item.type
        }).then(() => {
          this.$Message({
            type: 'success',
            message: '删除成功!'
          });
        }).catch(() => {
          this.$Message({
            message: '已取消删除'
          });
        });
      }
    }
  }
</script>

布局居中

使用 center 属性水平居中。

查看代码

<template>
  <w-button @click="messageShow">文字居中</w-button>
</template>

<script>
  export default {
    methods: {
      open() {
        this.$Confirm('此操作将永久删除该文件, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning',
          center: true
        }).then(() => {
          this.$Message({
            type: 'success',
            message: '删除成功!'
          });
        }).catch(() => {
          this.$Message({
            message: '已取消删除'
          });
        });
      }
    }
  }
</script>

# 全局方法

WinDesign 为 Vue.prototype 添加了全局方法 $Msgbox, $Alert, $Confirm 和 $Prompt。因此在 Vue instance 中可以采用本页面中的方式调用 MessageBox。调用参数为:




 

$Msgbox(options)
$Alert(message, title, options)$Alert(message, options)
$Confirm(message, title, options)$Confirm(message, options)
$Prompt(message, title, options)$Prompt(message, options)
1
2
3
4

# 单独引用

单独引入 MessageBox



import { MessageBox } from 'win-design'
1

那么对应于上述四个全局方法的调用方法依次为:MessageBox, MessageBox.alert, MessageBox.confirm 和 MessageBox.prompt,调用参数与全局方法相同。

# API

# Options

参数 说明 类型 可选值 默认值 Version
draggable 为 MessageBox 启用可拖拽功能 Boolean false v2.0.9
title MessageBox 标题 String
message MessageBox 消息正文内容 String
dangerouslyUseHTMLString 是否将 message 属性作为 HTML 片段处理 Boolean false
type 消息类型,用于显示图标 String 'success' | 'info' | 'warning' | 'error'
icon-class 自定义图标的类名,会覆盖 type String
custom-class MessageBox 的自定义类名 String
show-close MessageBox 是否显示右上角关闭按钮 Boolean true
show-cancel-button 是否显示取消按钮 Boolean true
show-confirm-button 是否显示确定按钮 Boolean true
cancel-button-text 取消按钮的文本内容 String 取消
confirm-button-text 确定按钮的文本内容 String 确定
cancel-button-class 取消按钮的自定义类名 String
confirm-button-class 确定按钮的自定义类名 String
close-on-click-modal 是否可通过点击遮罩关闭 MessageBox Boolean true
close-on-press-escape 是否可通过按下 ESC 键关闭 MessageBox Boolean true
center 是否居中布局 Boolean false
round-button 是否使用圆角按钮 Boolean false
lock-scroll 是否在 Modal 出现时将 body 滚动锁定 Boolean true
callback 若不使用 Promise,可以使用此参数指定 MessageBox 关闭后的回调 Function(action, instance),action 的值为'confirm', 'cancel'或'close', instance 为 MessageBox 实例,可以通过它访问实例上的属性和方法
before-close MessageBox 关闭前的回调,会暂停实例的关闭 Function(action, instance, done),action 的值为'confirm', 'cancel'或'close';instance 为 MessageBox 实例,可以通过它访问实例上的属性和方法;done 用于关闭 MessageBox 实例
distinguish-cancel-and-close 是否将取消(点击取消按钮)与关闭(点击关闭按钮或遮罩层、按下 ESC 键)进行区分 Boolean false
close-on-hash-change 是否在 hashchange 时关闭 MessageBox Boolean true
word-break 设置内容换行行为,尤其是出现多种语言 String - V2.4.29
overflow 是否可以拖拽到屏幕外 Boolean false V2.5.16
showInput 是否显示输入框 boolean false(以 prompt 方式调用时为 true)
inputPlaceholder 输入框的占位符 string
inputType 输入框的类型 string text
inputValue 输入框的初始文本 string
inputPattern 输入框的校验表达式 regexp
inputValidator 输入框的校验函数。可以返回布尔值或字符串,若返回一个字符串, 则返回结果会被赋值给 inputErrorMessage function
inputErrorMessage 校验未通过时的提示文本 string 输入的数据不合法!
上次更新: 10/21/2025, 9:55:04 AM