Modal对话框 - 图1

Modal 对话框

模态对话框。

何时使用

需要用户处理事务,又不希望跳转页面以致打断工作流程时,可以使用 Modal 在当前页面正中打开一个浮层,承载相应的操作。
另外当需要一个简洁的确认框询问用户时,可以使用 Modal.confirm() 等语法糖方法。

代码演示

Modal对话框 - 图2

异步关闭

点击确定后异步关闭对话框,例如提交表单。

  1. <template>
  2. <div>
  3. <a-button type="primary" @click="showModal">
  4. Open Modal with async logic
  5. </a-button>
  6. <a-modal
  7. title="Title"
  8. :visible="visible"
  9. :confirm-loading="confirmLoading"
  10. @ok="handleOk"
  11. @cancel="handleCancel"
  12. >
  13. <p>{{ ModalText }}</p>
  14. </a-modal>
  15. </div>
  16. </template>
  17. <script>
  18. export default {
  19. data() {
  20. return {
  21. ModalText: 'Content of the modal',
  22. visible: false,
  23. confirmLoading: false,
  24. };
  25. },
  26. methods: {
  27. showModal() {
  28. this.visible = true;
  29. },
  30. handleOk(e) {
  31. this.ModalText = 'The modal will be closed after two seconds';
  32. this.confirmLoading = true;
  33. setTimeout(() => {
  34. this.visible = false;
  35. this.confirmLoading = false;
  36. }, 2000);
  37. },
  38. handleCancel(e) {
  39. console.log('Clicked cancel button');
  40. this.visible = false;
  41. },
  42. },
  43. };
  44. </script>

Confirm

确认对话框(promise)

使用 confirm() 可以快捷地弹出确认框。onCancel/onOk 返回 promise 可以延迟关闭

  1. <template>
  2. <a-button @click="showConfirm">
  3. Confirm
  4. </a-button>
  5. </template>
  6. <script>
  7. export default {
  8. methods: {
  9. showConfirm() {
  10. this.$confirm({
  11. title: 'Do you want to delete these items?',
  12. content: 'When clicked the OK button, this dialog will be closed after 1 second',
  13. onOk() {
  14. return new Promise((resolve, reject) => {
  15. setTimeout(Math.random() > 0.5 ? resolve : reject, 1000);
  16. }).catch(() => console.log('Oops errors!'));
  17. },
  18. onCancel() {},
  19. });
  20. },
  21. },
  22. };
  23. </script>

Modal对话框 - 图3

自定义页脚

更复杂的例子,自定义了页脚的按钮,点击提交后进入 loading 状态,完成后关闭。
不需要默认确定取消按钮时,你可以把 footer 设为 null

  1. <template>
  2. <div>
  3. <a-button type="primary" @click="showModal">
  4. Open Modal with customized footer
  5. </a-button>
  6. <a-modal v-model="visible" title="Title" on-ok="handleOk">
  7. <template slot="footer">
  8. <a-button key="back" @click="handleCancel">
  9. Return
  10. </a-button>
  11. <a-button key="submit" type="primary" :loading="loading" @click="handleOk">
  12. Submit
  13. </a-button>
  14. </template>
  15. <p>Some contents...</p>
  16. <p>Some contents...</p>
  17. <p>Some contents...</p>
  18. <p>Some contents...</p>
  19. <p>Some contents...</p>
  20. </a-modal>
  21. </div>
  22. </template>
  23. <script>
  24. export default {
  25. data() {
  26. return {
  27. loading: false,
  28. visible: false,
  29. };
  30. },
  31. methods: {
  32. showModal() {
  33. this.visible = true;
  34. },
  35. handleOk(e) {
  36. this.loading = true;
  37. setTimeout(() => {
  38. this.visible = false;
  39. this.loading = false;
  40. }, 3000);
  41. },
  42. handleCancel(e) {
  43. this.visible = false;
  44. },
  45. },
  46. };
  47. </script>

Modal对话框 - 图4

国际化

设置 okTextcancelText 以自定义按钮文字。

  1. <template>
  2. <div>
  3. <a-button type="primary" @click="showModal">
  4. Modal
  5. </a-button>
  6. <a-modal v-model="visible" title="Modal" ok-text="确认" cancel-text="取消" @ok="hideModal">
  7. <p>Bla bla ...</p>
  8. <p>Bla bla ...</p>
  9. <p>Bla bla ...</p>
  10. </a-modal>
  11. <br />
  12. <br />
  13. <a-button @click="confirm">
  14. Confirm
  15. </a-button>
  16. </div>
  17. </template>
  18. <script>
  19. export default {
  20. data() {
  21. return {
  22. visible: false,
  23. };
  24. },
  25. methods: {
  26. showModal() {
  27. this.visible = true;
  28. },
  29. hideModal() {
  30. this.visible = false;
  31. },
  32. confirm() {
  33. this.$confirm({
  34. title: 'Confirm',
  35. content: 'Bla bla ...',
  36. okText: '确认',
  37. cancelText: '取消',
  38. });
  39. },
  40. },
  41. };
  42. </script>

Modal对话框 - 图5

自定义位置

使用 centered 或类似 dialogStyle.top 的样式来设置对话框位置。

  1. <template>
  2. <div id="components-modal-demo-position">
  3. <a-button type="primary" @click="() => setModal1Visible(true)">
  4. Display a modal dialog at 20px to Top
  5. </a-button>
  6. <a-modal
  7. title="20px to Top"
  8. :dialog-style="{ top: '20px' }"
  9. :visible="modal1Visible"
  10. @ok="() => setModal1Visible(false)"
  11. @cancel="() => setModal1Visible(false)"
  12. >
  13. <p>some contents...</p>
  14. <p>some contents...</p>
  15. <p>some contents...</p>
  16. </a-modal>
  17. <br /><br />
  18. <a-button type="primary" @click="() => (modal2Visible = true)">
  19. Vertically centered modal dialog
  20. </a-button>
  21. <a-modal
  22. v-model="modal2Visible"
  23. title="Vertically centered modal dialog"
  24. centered
  25. @ok="() => (modal2Visible = false)"
  26. >
  27. <p>some contents...</p>
  28. <p>some contents...</p>
  29. <p>some contents...</p>
  30. </a-modal>
  31. </div>
  32. </template>
  33. <script>
  34. export default {
  35. data() {
  36. return {
  37. modal1Visible: false,
  38. modal2Visible: false,
  39. };
  40. },
  41. methods: {
  42. setModal1Visible(modal1Visible) {
  43. this.modal1Visible = modal1Visible;
  44. },
  45. },
  46. };
  47. </script>

Confirm

销毁确认对话框

使用 Modal.destroyAll() 可以销毁弹出的确认窗。通常用于路由监听当中,处理路由前进、后退不能销毁确认对话框的问题。

  1. <template>
  2. <a-button @click="showConfirm">
  3. Confirm
  4. </a-button>
  5. </template>
  6. <script>
  7. export default {
  8. methods: {
  9. showConfirm() {
  10. const self = this;
  11. for (let i = 0; i < 3; i += 1) {
  12. setTimeout(() => {
  13. this.$confirm({
  14. content: 'destroy all',
  15. onOk() {
  16. return new Promise((resolve, reject) => {
  17. setTimeout(Math.random() > 0.5 ? resolve : reject, 1000);
  18. }).catch(() => console.log('Oops errors!'));
  19. },
  20. cancelText: 'Click to destroy all',
  21. onCancel() {
  22. self.destroyAll();
  23. },
  24. });
  25. }, i * 500);
  26. }
  27. },
  28. destroyAll() {
  29. this.$destroyAll();
  30. },
  31. },
  32. };
  33. </script>

Modal对话框 - 图6

基本

第一个对话框。

  1. <template>
  2. <div>
  3. <a-button type="primary" @click="showModal">
  4. Open Modal
  5. </a-button>
  6. <a-modal v-model="visible" title="Basic Modal" @ok="handleOk">
  7. <p>Some contents...</p>
  8. <p>Some contents...</p>
  9. <p>Some contents...</p>
  10. </a-modal>
  11. </div>
  12. </template>
  13. <script>
  14. export default {
  15. data() {
  16. return {
  17. visible: false,
  18. };
  19. },
  20. methods: {
  21. showModal() {
  22. this.visible = true;
  23. },
  24. handleOk(e) {
  25. console.log(e);
  26. this.visible = false;
  27. },
  28. },
  29. };
  30. </script>

Modal对话框 - 图7

确认对话框

使用 confirm() 可以快捷地弹出确认框。

  1. <template>
  2. <div>
  3. <a-button @click="showConfirm">
  4. Confirm
  5. </a-button>
  6. <a-button type="dashed" @click="showDeleteConfirm">
  7. Delete
  8. </a-button>
  9. <a-button type="dashed" @click="showPropsConfirm">
  10. With extra props
  11. </a-button>
  12. </div>
  13. </template>
  14. <script>
  15. export default {
  16. methods: {
  17. showConfirm() {
  18. this.$confirm({
  19. title: 'Do you Want to delete these items?',
  20. content: h => <div style="color:red;">Some descriptions</div>,
  21. onOk() {
  22. console.log('OK');
  23. },
  24. onCancel() {
  25. console.log('Cancel');
  26. },
  27. class: 'test',
  28. });
  29. },
  30. showDeleteConfirm() {
  31. this.$confirm({
  32. title: 'Are you sure delete this task?',
  33. content: 'Some descriptions',
  34. okText: 'Yes',
  35. okType: 'danger',
  36. cancelText: 'No',
  37. onOk() {
  38. console.log('OK');
  39. },
  40. onCancel() {
  41. console.log('Cancel');
  42. },
  43. });
  44. },
  45. showPropsConfirm() {
  46. this.$confirm({
  47. title: 'Are you sure delete this task?',
  48. content: 'Some descriptions',
  49. okText: 'Yes',
  50. okType: 'danger',
  51. okButtonProps: {
  52. props: { disabled: true },
  53. },
  54. cancelText: 'No',
  55. onOk() {
  56. console.log('OK');
  57. },
  58. onCancel() {
  59. console.log('Cancel');
  60. },
  61. });
  62. },
  63. },
  64. };
  65. </script>

Modal对话框 - 图8

信息提示

各种类型的信息提示,只提供一个按钮用于关闭。

  1. <template>
  2. <div>
  3. <a-button @click="info">
  4. Info
  5. </a-button>
  6. <a-button @click="success">
  7. Success
  8. </a-button>
  9. <a-button @click="error">
  10. Error
  11. </a-button>
  12. <a-button @click="warning">
  13. Warning
  14. </a-button>
  15. </div>
  16. </template>
  17. <script>
  18. import { Modal } from 'ant-design-vue';
  19. export default {
  20. methods: {
  21. info() {
  22. const h = this.$createElement;
  23. this.$info({
  24. title: 'This is a notification message',
  25. content: h('div', {}, [
  26. h('p', 'some messages...some messages...'),
  27. h('p', 'some messages...some messages...'),
  28. ]),
  29. onOk() {},
  30. });
  31. },
  32. success() {
  33. this.$success({
  34. title: 'This is a success message',
  35. // JSX support
  36. content: (
  37. <div>
  38. <p>some messages...some messages...</p>
  39. <p>some messages...some messages...</p>
  40. </div>
  41. ),
  42. });
  43. },
  44. error() {
  45. this.$error({
  46. title: 'This is an error message',
  47. content: 'some messages...some messages...',
  48. });
  49. },
  50. warning() {
  51. this.$warning({
  52. title: 'This is a warning message',
  53. content: 'some messages...some messages...',
  54. });
  55. },
  56. },
  57. };
  58. </script>

Open modal to close in 5s

手动更新和移除

手动更新和关闭 Modal.method 方式创建的对话框。

  1. <template>
  2. <a-button @click="countDown">
  3. Open modal to close in 5s
  4. </a-button>
  5. </template>
  6. <script>
  7. export default {
  8. methods: {
  9. countDown() {
  10. let secondsToGo = 5;
  11. const modal = this.$success({
  12. title: 'This is a notification message',
  13. content: `This modal will be destroyed after ${secondsToGo} second.`,
  14. });
  15. const interval = setInterval(() => {
  16. secondsToGo -= 1;
  17. modal.update({
  18. content: `This modal will be destroyed after ${secondsToGo} second.`,
  19. });
  20. }, 1000);
  21. setTimeout(() => {
  22. clearInterval(interval);
  23. modal.destroy();
  24. }, secondsToGo * 1000);
  25. },
  26. },
  27. };
  28. </script>

Modal对话框 - 图9

自定义页脚按钮属性

传入 okButtonPropscancelButtonProps 可分别自定义确定按钮和取消按钮的 props。

  1. <template>
  2. <div>
  3. <a-button type="primary" @click="showModal">
  4. Open Modal with customized button props
  5. </a-button>
  6. <a-modal
  7. v-model="visible"
  8. title="Basic Modal"
  9. :ok-button-props="{ props: { disabled: true } }"
  10. :cancel-button-props="{ props: { disabled: true } }"
  11. @ok="handleOk"
  12. >
  13. <p>Some contents...</p>
  14. <p>Some contents...</p>
  15. <p>Some contents...</p>
  16. </a-modal>
  17. </div>
  18. </template>
  19. <script>
  20. export default {
  21. data() {
  22. return {
  23. visible: false,
  24. };
  25. },
  26. methods: {
  27. showModal() {
  28. this.visible = true;
  29. },
  30. handleOk(e) {
  31. console.log(e);
  32. this.visible = false;
  33. },
  34. handleCancel(e) {
  35. console.log(e);
  36. this.visible = false;
  37. },
  38. },
  39. };
  40. </script>

API

参数说明类型默认值版本
afterCloseModal 完全关闭后的回调function
bodyStyleModal body 样式object{}
cancelText取消按钮文字string| slot取消
centered垂直居中展示 ModalBooleanfalse
closable是否显示右上角的关闭按钮booleantrue
closeIcon自定义关闭图标VNode | slot-1.5.0
confirmLoading确定按钮 loadingboolean
destroyOnClose关闭时销毁 Modal 里的子元素booleanfalse
footer底部内容,当不需要默认底部按钮时,可以设为 :footer=”null”string|slot确定取消按钮
forceRender强制渲染 Modalbooleanfalse
getContainer指定 Modal 挂载的 HTML 节点(instance): HTMLElement() => document.body
keyboard是否支持键盘 esc 关闭booleantrue
mask是否展示遮罩Booleantrue
maskClosable点击蒙层是否允许关闭booleantrue
maskStyle遮罩样式object{}
okText确认按钮文字string|slot确定
okType确认按钮类型stringprimary
okButtonPropsok 按钮 props, 遵循 jsx规范{props: ButtonProps, on: {}}-
cancelButtonPropscancel 按钮 props, 遵循 jsx规范{props: ButtonProps, on: {}}-
title标题string|slot
visible(v-model)对话框是否可见boolean
width宽度string|number520
wrapClassName对话框外层容器的类名string-
zIndex设置 Modal 的 z-indexNumber1000
dialogStyle可用于设置浮层的样式,调整浮层位置等object-1.6.1
dialogClass可用于设置浮层的类名string-1.6.1

事件

事件名称说明回调参数
cancel点击遮罩层或右上角叉或取消按钮的回调function(e)
ok点击确定回调function(e)

注意

<Modal /> 默认关闭后状态不会自动清空, 如果希望每次打开都是新内容,请设置 destroyOnClose

Modal.method()

包括:

  • Modal.info
  • Modal.success
  • Modal.error
  • Modal.warning
  • Modal.confirm

以上均为一个函数,参数为 object,具体属性如下:

参数说明类型默认值版本
autoFocusButton指定自动获得焦点的按钮null|string: ok cancelok
cancelText取消按钮文字string取消
centered垂直居中展示 ModalBooleanfalse
closable是否显示右上角的关闭按钮booleanfalse
class容器类名string-
content内容string |vNode |function(h)
icon自定义图标(1.14.0 新增)string|()=>VNode<Icon type=”question-circle”>
iconType图标类型(1.14.0 后废弃,请使用 iconstringquestion-circle
mask是否展示遮罩Booleantrue
maskClosable点击蒙层是否允许关闭Booleanfalse
keyboard是否支持键盘 esc 关闭booleantrue
okText确认按钮文字string确定
okType确认按钮类型stringprimary
okButtonPropsok 按钮 propsButtonProps-
cancelButtonPropscancel 按钮 propsButtonProps-
title标题string|vNode |function(h)
width宽度string|number416
zIndex设置 Modal 的 z-indexNumber1000
onCancel取消回调,参数为关闭函数,返回 promise 时 resolve 后自动关闭function
onOk点击确定回调,参数为关闭函数,返回 promise 时 resolve 后自动关闭function
parentContext弹窗的父级上下文,一般用于获取父级 provider, 如获取 ConfigProvider 的配置vue instance-1.4.11

以上函数调用后,会返回一个引用,可以通过该引用更新和关闭弹窗。

  1. const modal = Modal.info();
  2. modal.update({
  3. title: '修改的标题',
  4. content: '修改的内容',
  5. });
  6. modal.destroy();
  • Modal.destroyAll

使用 Modal.destroyAll() 可以销毁弹出的确认窗(即上述的 Modal.info、Modal.success、Modal.error、Modal.warning、Modal.confirm)。通常用于路由监听当中,处理路由前进、后退不能销毁确认对话框的问题,而不用各处去使用实例的返回值进行关闭(modal.destroy() 适用于主动关闭,而不是路由这样被动关闭)

  1. const router = new VueRouter({ ... })
  2. // router change
  3. router.beforeEach((to, from, next) => {
  4. Modal.destroyAll();
  5. })