• Modal 对话框
    • 何时使用
    • 代码演示
      • 异步关闭
      • 基本
      • 确认对话框(promise)
      • 确认对话框
      • 自定义页脚
      • 信息提示
      • 国际化
      • 手动更新和移除
      • 自定义位置
      • 自定义页脚按钮属性
  • API
    • 事件
      • 注意
    • Modal.method()

    Modal 对话框

    模态对话框。

    何时使用

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

    代码演示

    Modal对话框 - 图1

    异步关闭

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

    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. :visible="visible"
    7. @ok="handleOk"
    8. :confirmLoading="confirmLoading"
    9. @cancel="handleCancel"
    10. >
    11. <p>{{ModalText}}</p>
    12. </a-modal>
    13. </div>
    14. </template>
    15. <script>
    16. export default {
    17. data() {
    18. return {
    19. ModalText: 'Content of the modal',
    20. visible: false,
    21. confirmLoading: false,
    22. }
    23. },
    24. methods: {
    25. showModal() {
    26. this.visible = true
    27. },
    28. handleOk(e) {
    29. this.ModalText = 'The modal will be closed after two seconds';
    30. this.confirmLoading = true;
    31. setTimeout(() => {
    32. this.visible = false;
    33. this.confirmLoading = false;
    34. }, 2000);
    35. },
    36. handleCancel(e) {
    37. console.log('Clicked cancel button');
    38. this.visible = false
    39. },
    40. }
    41. }
    42. </script>

    Modal对话框 - 图2

    基本

    第一个对话框。

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

    Modal对话框 - 图3

    确认对话框(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对话框 - 图4

    确认对话框

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

    1. <template>
    2. <div>
    3. <a-button @click="showConfirm">
    4. Confirm
    5. </a-button>
    6. <a-button @click="showDeleteConfirm" type="dashed">
    7. Delete
    8. </a-button>
    9. <a-button @click="showPropsConfirm" type="dashed">
    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: 'Some descriptions',
    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对话框 - 图5

    自定义页脚

    更复杂的例子,自定义了页脚的按钮,点击提交后进入 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
    7. v-model="visible"
    8. title="Title"
    9. onOk="handleOk"
    10. >
    11. <template slot="footer">
    12. <a-button key="back" @click="handleCancel">Return</a-button>
    13. <a-button key="submit" type="primary" :loading="loading" @click="handleOk">
    14. Submit
    15. </a-button>
    16. </template>
    17. <p>Some contents...</p>
    18. <p>Some contents...</p>
    19. <p>Some contents...</p>
    20. <p>Some contents...</p>
    21. <p>Some contents...</p>
    22. </a-modal>
    23. </div>
    24. </template>
    25. <script>
    26. export default {
    27. data() {
    28. return {
    29. loading: false,
    30. visible: false,
    31. }
    32. },
    33. methods: {
    34. showModal() {
    35. this.visible = true;
    36. },
    37. handleOk(e) {
    38. this.loading = true;
    39. setTimeout(() => {
    40. this.visible = false;
    41. this.loading = false;
    42. }, 3000);
    43. },
    44. handleCancel(e) {
    45. this.visible = false;
    46. },
    47. }
    48. }
    49. </script>

    Modal对话框 - 图6

    信息提示

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

    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>
    10. import { Modal } from 'ant-design-vue'
    11. export default {
    12. methods: {
    13. info() {
    14. const h = this.$createElement
    15. this.$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. });
    23. },
    24. success() {
    25. this.$success({
    26. title: 'This is a success message',
    27. content: ( // JSX support
    28. <div>
    29. <p>some messages...some messages...</p>
    30. <p>some messages...some messages...</p>
    31. </div>
    32. ),
    33. });
    34. },
    35. error() {
    36. this.$error({
    37. title: 'This is an error message',
    38. content: 'some messages...some messages...',
    39. });
    40. },
    41. warning() {
    42. this.$warning({
    43. title: 'This is a warning message',
    44. content: 'some messages...some messages...',
    45. });
    46. },
    47. }
    48. }
    49. </script>

    Modal对话框 - 图7

    国际化

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

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

    Modal对话框 - 图8

    手动更新和移除

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

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

    Modal对话框 - 图9

    自定义位置

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

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

    API

    参数说明类型默认值
    afterCloseModal 完全关闭后的回调function
    bodyStyleModal body 样式object{}
    cancelText取消按钮文字string| slot取消
    centered垂直居中展示 ModalBooleanfalse
    closable是否显示右上角的关闭按钮booleantrue
    confirmLoading确定按钮 loadingboolean
    destroyOnClose关闭时销毁 Modal 里的子元素booleanfalse
    footer底部内容,当不需要默认底部按钮时,可以设为 :footer="null"string|slot确定取消按钮
    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

    事件

    事件名称说明回调参数
    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
    class容器类名string-
    content内容string|vNode
    iconType图标 Icon 类型stringquestion-circle
    maskClosable点击蒙层是否允许关闭Booleanfalse
    keyboard是否支持键盘esc关闭booleantrue
    okText确认按钮文字string确定
    okType确认按钮类型stringprimary
    okButtonPropsok 按钮 propsButtonProps-
    cancelButtonPropscancel 按钮 propsButtonProps-
    title标题string|vNode
    width宽度string|number416
    zIndex设置 Modal 的 z-indexNumber1000
    onCancel取消回调,参数为关闭函数,返回 promise 时 resolve 后自动关闭function
    onOk点击确定回调,参数为关闭函数,返回 promise 时 resolve 后自动关闭function

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

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