Modal对话框 - 图1

Modal 对话框

模态对话框。

何时使用

需要用户处理事务,又不希望跳转页面以致打断工作流程时,可以使用 Modal 在当前页面正中打开一个浮层,承载相应的操作。

另外当需要一个简洁的确认框询问用户时,可以使用 Modal.confirm() 等语法糖方法。

代码演示

Modal对话框 - 图2

基本用法

第一个对话框。

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

Modal对话框 - 图3

自定义页脚

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

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

Modal对话框 - 图4

信息提示

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

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

Open modal to close in 5s

手动更新和移除

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

  1. <template>
  2. <a-button @click="countDown">Open modal to close in 5s</a-button>
  3. </template>
  4. <script lang="ts">
  5. import { Modal } from 'ant-design-vue';
  6. import { defineComponent } from 'vue';
  7. export default defineComponent({
  8. setup() {
  9. const countDown = () => {
  10. let secondsToGo = 5;
  11. const modal = Modal.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. return {
  27. countDown,
  28. };
  29. },
  30. });
  31. </script>

Confirm

销毁确认对话框

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

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

Modal对话框 - 图5

自定义模态的宽度

使用width来设置模态对话框的宽度

  1. <template>
  2. <div>
  3. <a-button type="primary" @click="showModal">Open Modal of 1000px width</a-button>
  4. <a-modal v-model:visible="visible" width="1000px" title="Basic Modal" @ok="handleOk">
  5. <p>Some contents...</p>
  6. <p>Some contents...</p>
  7. <p>Some contents...</p>
  8. </a-modal>
  9. </div>
  10. </template>
  11. <script lang="ts">
  12. import { defineComponent, ref } from 'vue';
  13. export default defineComponent({
  14. setup() {
  15. const visible = ref<boolean>(false);
  16. const showModal = () => {
  17. visible.value = true;
  18. };
  19. const handleOk = (e: MouseEvent) => {
  20. console.log(e);
  21. visible.value = false;
  22. };
  23. return {
  24. visible,
  25. showModal,
  26. handleOk,
  27. };
  28. },
  29. });
  30. </script>

Modal对话框 - 图6

异步关闭

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

  1. <template>
  2. <div>
  3. <a-button type="primary" @click="showModal">Open Modal with async logic</a-button>
  4. <a-modal
  5. title="Title"
  6. v-model:visible="visible"
  7. :confirm-loading="confirmLoading"
  8. @ok="handleOk"
  9. >
  10. <p>{{ modalText }}</p>
  11. </a-modal>
  12. </div>
  13. </template>
  14. <script lang="ts">
  15. import { ref, defineComponent } from 'vue';
  16. export default defineComponent({
  17. setup() {
  18. const modalText = ref<string>('Content of the modal');
  19. const visible = ref<boolean>(false);
  20. const confirmLoading = ref<boolean>(false);
  21. const showModal = () => {
  22. visible.value = true;
  23. };
  24. const handleOk = () => {
  25. modalText.value = 'The modal will be closed after two seconds';
  26. confirmLoading.value = true;
  27. setTimeout(() => {
  28. visible.value = false;
  29. confirmLoading.value = false;
  30. }, 2000);
  31. };
  32. return {
  33. modalText,
  34. visible,
  35. confirmLoading,
  36. showModal,
  37. handleOk,
  38. };
  39. },
  40. });
  41. </script>

Modal对话框 - 图7

确认对话框

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

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

Modal对话框 - 图8

国际化

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

  1. <template>
  2. <div>
  3. <a-button type="primary" @click="showModal">Modal</a-button>
  4. <a-button @click="confirm">Confirm</a-button>
  5. <a-modal
  6. v-model:visible="visible"
  7. title="Modal"
  8. ok-text="确认"
  9. cancel-text="取消"
  10. @ok="hideModal"
  11. >
  12. <p>Bla bla ...</p>
  13. <p>Bla bla ...</p>
  14. <p>Bla bla ...</p>
  15. </a-modal>
  16. </div>
  17. </template>
  18. <script lang="ts">
  19. import { ExclamationCircleOutlined } from '@ant-design/icons-vue';
  20. import { defineComponent, ref, createVNode } from 'vue';
  21. import { Modal } from 'ant-design-vue';
  22. export default defineComponent({
  23. setup() {
  24. const visible = ref<boolean>(false);
  25. const showModal = () => {
  26. visible.value = true;
  27. };
  28. const hideModal = () => {
  29. visible.value = false;
  30. };
  31. const confirm = () => {
  32. Modal.confirm({
  33. title: 'Confirm',
  34. icon: createVNode(ExclamationCircleOutlined),
  35. content: 'Bla bla ...',
  36. okText: '确认',
  37. cancelText: '取消',
  38. });
  39. };
  40. return {
  41. visible,
  42. showModal,
  43. hideModal,
  44. confirm,
  45. };
  46. },
  47. });
  48. </script>

Modal对话框 - 图9

自定义位置

使用 centered 或类似 style.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. style="top: 20px"
  9. v-model:visible="modal1Visible"
  10. @ok="setModal1Visible(false)"
  11. >
  12. <p>some contents...</p>
  13. <p>some contents...</p>
  14. <p>some contents...</p>
  15. </a-modal>
  16. <br />
  17. <br />
  18. <a-button type="primary" @click="modal2Visible = true">
  19. Vertically centered modal dialog
  20. </a-button>
  21. <a-modal
  22. v-model:visible="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 lang="ts">
  34. import { defineComponent, ref } from 'vue';
  35. export default defineComponent({
  36. setup() {
  37. const modal1Visible = ref<boolean>(false);
  38. const modal2Visible = ref<boolean>(false);
  39. const setModal1Visible = (visible: boolean) => {
  40. modal1Visible.value = visible;
  41. };
  42. return {
  43. modal1Visible,
  44. modal2Visible,
  45. setModal1Visible,
  46. };
  47. },
  48. });
  49. </script>

Confirm

确认对话框(promise)

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

  1. <template>
  2. <a-button @click="showConfirm">Confirm</a-button>
  3. </template>
  4. <script lang="ts">
  5. import { ExclamationCircleOutlined } from '@ant-design/icons-vue';
  6. import { createVNode, defineComponent } from 'vue';
  7. import { Modal } from 'ant-design-vue';
  8. export default defineComponent({
  9. setup() {
  10. const showConfirm = () => {
  11. Modal.confirm({
  12. title: 'Do you want to delete these items?',
  13. icon: createVNode(ExclamationCircleOutlined),
  14. content: 'When clicked the OK button, this dialog will be closed after 1 second',
  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. // eslint-disable-next-line @typescript-eslint/no-empty-function
  21. onCancel() {},
  22. });
  23. };
  24. return {
  25. showConfirm,
  26. };
  27. },
  28. });
  29. </script>

Modal对话框 - 图10

自定义页脚按钮属性

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

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

API

参数说明类型默认值版本
afterCloseModal 完全关闭后的回调function
bodyStyleModal body 样式object{}
cancelText取消按钮文字string| slot取消
centered垂直居中展示 ModalBooleanfalse
closable是否显示右上角的关闭按钮booleantrue
closeIcon自定义关闭图标VNode | slot-
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 按钮 propsButtonProps-
cancelButtonPropscancel 按钮 propsButtonProps-
title标题string|slot
visible(v-model)对话框是否可见boolean
width宽度string|number520
wrapClassName对话框外层容器的类名string-
zIndex设置 Modal 的 z-indexNumber1000
dialogStyle可用于设置浮层的样式,调整浮层位置等object-
dialogClass可用于设置浮层的类名string-

事件

事件名称说明回调参数
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 新增)VNode | ()=>VNode-
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. 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. })