Modal 模态框


模态对话框,当需要询问用户处理事务,又不希望跳转页面时,可以使用模态框 Modal 在当前页面打开一个浮层并承载相应的操作。

当需要弹出一个简洁的确认框时,也可以使用默认的精简版模态框。AT-UIVue.prototype 中添加了全局对象 $Modal,可以直接通过 this.$Modal 对象操作实例方法

Modal 实例方法

通过调用 this.$Modal 的方法来使用:

  • this.$Modal.alert(config)
  • this.$Modal.confirm(config)
  • this.$Modal.prompt(config)
  • this.$Modal.info(config)
  • this.$Modal.success(config)
  • this.$Modal.warning(config)
  • this.$Modal.error(config)

消息提醒

弹出会中断用户的对话框,直到用户知晓该信息之后才可以关闭,属于交互比较重的操作。(类似于 window.alert

可以用 Promise 的方式捕获操作反馈,也可以用传入 callback 参数的方式

Modal模态框 - 图1

  1. <p class="demo-desc">this.$Modal.alert()</p>
  2. <at-button @click="modalAlert">Alert</at-button>
  3. <script>
  4. export default {
  5. methods: {
  6. modalAlert () {
  7. this.$Modal.alert({
  8. title: '这里是标题名称',
  9. content: '这里是文本内容',
  10. callback: function (action) {
  11. this.$Message(action)
  12. }
  13. })
  14. }
  15. }
  16. }
  17. </script>

确认消息

对用户操作的一个反馈,用于确定是否需要继续操作。(类似于 window.confirm

Modal模态框 - 图2

  1. <p class="demo-desc">this.$Modal.confirm()</p>
  2. <at-button @click="modalConfirm">Confirm</at-button>
  3. <script>
  4. export default {
  5. methods: {
  6. modalConfirm () {
  7. this.$Modal.confirm({
  8. title: '提示',
  9. content: '此操作需要非常谨慎,您确定要这么做吗?'
  10. }).then(() => {
  11. this.$Message('点击了「确认」按钮')
  12. }).catch(() => {
  13. this.$Message('点击了「取消」按钮')
  14. })
  15. }
  16. }
  17. }
  18. </script>

提交信息

弹出输入对话框,提醒用户输入相应内容。(类似于 window.prompt

Modal模态框 - 图3

  1. <p class="demo-desc">this.$Modal.prompt({ title: '提示', content: '请输入邮件地址:' })</p>
  2. <at-button @click="modalPrompt">Prompt</at-button>
  3. <script>
  4. export default {
  5. methods: {
  6. modalPrompt () {
  7. this.$Modal.prompt({
  8. title: '提示',
  9. content: '请输入邮件地址:'
  10. }).then((data) => {
  11. this.$Message(`点击了「确认」按钮,输入框的值为 ${data.value}`)
  12. }).catch(() => {
  13. this.$Message('点击了「取消」按钮')
  14. })
  15. }
  16. }
  17. }
  18. </script>

消息类的对话框

除了上述的类 window 对话框,AT-UI 还提供了四种消息类的对话框,主要用来展示一些重要信息。该类的对话框仅允许点击「确定」按钮关闭,不支持其他关闭方式

Modal模态框 - 图4

  1. <p class="demo-desc">this.$Modal.success()</p>
  2. <at-button @click="handleClick('success')">成功</at-button>
  3. <at-button @click="handleClick('error')">错误</at-button>
  4. <at-button @click="handleClick('warning')">警告</at-button>
  5. <at-button @click="handleClick('info')">消息</at-button>
  6. <script>
  7. export default {
  8. methods: {
  9. handleClick (type) {
  10. if (type === 'info') {
  11. this.$Modal.info({
  12. content: '这里是提示的消息'
  13. })
  14. } else if (type === 'success') {
  15. this.$Modal.success({
  16. content: '这里是成功的消息'
  17. })
  18. } else if (type === 'warning') {
  19. this.$Modal.warning({
  20. content: '这里是警告的消息'
  21. })
  22. } else if (type === 'error') {
  23. this.$Modal.error({
  24. content: '这里是错误的消息'
  25. })
  26. }
  27. }
  28. }
  29. }
  30. </script>

组件化方式调用

前面提到的是通过 this.$Modal 的方法来使用,如果要自定义对话框,可使用组件化的方式

Modal模态框 - 图5

  1. <at-button @click="modal1=true">显示自定义模态框</at-button>
  2. <at-modal v-model="modal1" title="这里是标题" @on-confirm="handleConfirm" @on-cancel="handleCancel">
  3. <p>这里是模态框的文本内容!</p>
  4. <p>这里是模态框的文本内容!</p>
  5. </at-modal>
  6. <script>
  7. export default {
  8. methods: {
  9. handleConfirm () {
  10. this.$Message('Confirm')
  11. },
  12. handleCancel () {
  13. this.$Message('Cancel')
  14. }
  15. }
  16. }
  17. </script>

自定义样式

Modal 组件提供了自定义页头、页脚的 slot,可灵活的控制对话框的样式结构。通过与其他组件的交互,可实现复杂的功能需求。

Modal模态框 - 图6

  1. <at-button @click="modal2=true">自定义页头和页脚</at-button>
  2. <at-button @click="modal3=true">不带标题</at-button>
  3. <at-modal v-model="modal2">
  4. <div slot="header" style="text-align:center;">
  5. <span>这里是标题</span>
  6. </div>
  7. <div style="text-align:center;">
  8. <p>能看到这里的内容吗?</p>
  9. </div>
  10. <div slot="footer">
  11. <at-button style="width:100%;" type="error" @click="closeModal2">这里是按钮</at-button>
  12. </div>
  13. </at-modal>
  14. <at-modal v-model="modal3">
  15. <p>这里是模态框的文本内容!</p>
  16. </at-modal>
  17. <script>
  18. export default {
  19. methods: {
  20. closeModal2 () {
  21. this.modal2 = false
  22. }
  23. }
  24. }
  25. </script>

禁用关闭

  • 设置属性 show-closefalse 可取消右上角的关闭按钮以及键盘的 ESC 键;
  • 设置属性 mask-closablefalse 可取消遮罩层的点击关闭事件;

Modal模态框 - 图7

<at-button @click="modal4=true">禁用右上角关闭按钮(含 ESC)</at-button>
<at-button @click="modal5=true">取消遮罩层关闭</at-button>
<at-modal v-model="modal4" title="标题" :show-close="false">这里是文本</at-modal>
<at-modal v-model="modal5" title="标题" :show-close="false" :mask-closable="false">这里是文本</at-modal>

自定义窗口位置

通过属性 styles 传入 CSS Style Object,可更改弹框的样式

Modal模态框 - 图8

<at-button @click="modal6=true">仅改变距离顶部的位置</at-button>
<at-modal v-model="modal6" title="标题" :styles="{top: '20px'}">这里是文本内容</at-modal>

关闭前

  • 通过属性 before-close 监听 Modal 关闭前的事件,会暂停 Modal 的关闭
  • 参数为event事件和回调函数,调用函数将关闭 Modal ,传递 false 参数可以阻止 Modal的关闭
  • 点击按钮、icon、遮罩、esc均会触发该钩子,手动修改value的值不触发

Modal模态框 - 图9

<at-button @click="modal7=true">打开modal</at-button>
<at-modal v-model="modal7" title="标题" :before-close="handleBeforeClose">这里是文本</at-modal>

Modal 参数

参数说明类型可选值默认值
value是否显示模态框,可通过 v-model 绑定Boolean-false
title模态框的标题String--
content模态框的内容String--
cancelText取消按钮的文本String-取消
okText确定按钮的文本String-确定
maskClosable点击遮罩层是否可以关闭模态框Boolean-true
showHead是否显示标题Boolean-true
showClose是否显示关闭按钮Boolean-true
showFooter是否显示底部按钮Boolean-true
showInput是否显示输入框Boolean-false
width模态框的宽度Number / String-520
closeOnPressEsc点击 ESC 是否可以关闭模态框Boolean-true
styles模态框的自定义样式Object--
before-close关闭前的回调,会暂停 Modal 的关闭,手动修改 value 的值不会触发Function(event, done),done 用于关闭 Modal,传递 false 参数可以阻止 Modal 关闭--

Modal 事件

事件名称说明返回参数
on-cancel点击取消的回调事件-
on-confirm点击确定的回调事件-

Modal Slots

名称说明
header自定义模态框的头部
footer自定义模态框的底部,即底部按钮部分
-自定义模态框的主体内容