# 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>

可拖拽

查看代码

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

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

提交内容

调用$Prompt方法即可打开消息提示,它模拟了系统的 prompt。可以用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
dangerously-use-html-String 是否将 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
上次更新: 5/8/2024, 10:14:04 AM