Modal对话框

Modal 对话框

模态对话框。

何时使用

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

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

代码演示

基本用法

第一个对话框。

<​template>  <​div>    <​a-button type="primary" @click="showModal">Open Modal<​/a-button>    <​a-modal v-model:visible="visible" title="Basic Modal" @ok="handleOk">      <​p>Some contents...<​/p>      <​p>Some contents...<​/p>      <​p>Some contents...<​/p>    <​/a-modal>  <​/div><​/template><​script lang="ts">import { defineComponent, ref } from 'vue';export default defineComponent({  setup() {    const visible = ref<​boolean>(false);    const showModal = () => {      visible.value = true;    };    const handleOk = (e: MouseEvent) => {      console.log(e);      visible.value = false;    };    return {      visible,      showModal,      handleOk,    };  },});<​/script>

自定义页脚

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

<​template>  <​div>    <​a-button type="primary" @click="showModal">Open Modal with customized footer<​/a-button>    <​a-modal v-model:visible="visible" title="Title" @ok="handleOk">      <​template #footer>        <​a-button key="back" @click="handleCancel">Return<​/a-button>        <​a-button key="submit" type="primary" :loading="loading" @click="handleOk">Submit<​/a-button>      <​/template>      <​p>Some contents...<​/p>      <​p>Some contents...<​/p>      <​p>Some contents...<​/p>      <​p>Some contents...<​/p>      <​p>Some contents...<​/p>    <​/a-modal>  <​/div><​/template><​script lang="ts">import { defineComponent, ref } from 'vue';export default defineComponent({  setup() {    const loading = ref<​boolean>(false);    const visible = ref<​boolean>(false);    const showModal = () => {      visible.value = true;    };    const handleOk = () => {      loading.value = true;      setTimeout(() => {        loading.value = false;        visible.value = false;      }, 2000);    };    const handleCancel = () => {      visible.value = false;    };    return {      loading,      visible,      showModal,      handleOk,      handleCancel,    };  },});<​/script>

信息提示

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

<​template>  <​div>    <​a-button @click="info">Info<​/a-button>    <​a-button @click="success">Success<​/a-button>    <​a-button @click="error">Error<​/a-button>    <​a-button @click="warning">Warning<​/a-button>  <​/div><​/template><​script lang="ts">import { Modal } from 'ant-design-vue';import { defineComponent, h } from 'vue';export default defineComponent({  setup() {    const info = () => {      Modal.info({        title: 'This is a notification message',        content: h('div', {}, [          h('p', 'some messages...some messages...'),          h('p', 'some messages...some messages...'),        ]),        onOk() {          console.log('ok');        },      });    };    const success = () => {      Modal.success({        title: 'This is a success message',        content: h('div', {}, [          h('p', 'some messages...some messages...'),          h('p', 'some messages...some messages...'),        ]),      });    };    const error = () => {      Modal.error({        title: 'This is an error message',        content: 'some messages...some messages...',      });    };    const warning = () => {      Modal.warning({        title: 'This is a warning message',        content: 'some messages...some messages...',      });    };    return {      info,      success,      error,      warning,    };  },});<​/script>

Open modal to close in 5s

手动更新和移除

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

<​template>  <​a-button @click="countDown">Open modal to close in 5s<​/a-button><​/template><​script lang="ts">import { Modal } from 'ant-design-vue';import { defineComponent } from 'vue';export default defineComponent({  setup() {    const countDown = () => {      let secondsToGo = 5;      const modal = Modal.success({        title: 'This is a notification message',        content: `This modal will be destroyed after ${secondsToGo} second.`,      });      const interval = setInterval(() => {        secondsToGo -= 1;        modal.update({          content: `This modal will be destroyed after ${secondsToGo} second.`,        });      }, 1000);      setTimeout(() => {        clearInterval(interval);        modal.destroy();      }, secondsToGo * 1000);    };    return {      countDown,    };  },});<​/script>

Confirm

销毁确认对话框

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

<​template>  <​a-button @click="showConfirm">Confirm<​/a-button><​/template><​script lang="ts">import { ExclamationCircleOutlined } from '@ant-design/icons-vue';import { createVNode, defineComponent } from 'vue';import { Modal } from 'ant-design-vue';export default defineComponent({  setup() {    const showConfirm = () => {      for (let i = 0; i < 3; i += 1) {        setTimeout(() => {          Modal.confirm({            content: 'destroy all',            icon: createVNode(ExclamationCircleOutlined),            onOk() {              return new Promise((resolve, reject) => {                setTimeout(Math.random() > 0.5 ? resolve : reject, 1000);              }).catch(() => console.log('Oops errors!'));            },            cancelText: 'Click to destroy all',            onCancel() {              Modal.destroyAll();            },          });        }, i * 500);      }    };    return {      showConfirm,    };  },});<​/script>

自定义模态的宽度

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

<​template>  <​div>    <​a-button type="primary" @click="showModal">Open Modal of 1000px width<​/a-button>    <​a-modal v-model:visible="visible" width="1000px" title="Basic Modal" @ok="handleOk">      <​p>Some contents...<​/p>      <​p>Some contents...<​/p>      <​p>Some contents...<​/p>    <​/a-modal>  <​/div><​/template><​script lang="ts">import { defineComponent, ref } from 'vue';export default defineComponent({  setup() {    const visible = ref<​boolean>(false);    const showModal = () => {      visible.value = true;    };    const handleOk = (e: MouseEvent) => {      console.log(e);      visible.value = false;    };    return {      visible,      showModal,      handleOk,    };  },});<​/script>

异步关闭

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

<​template>  <​div>    <​a-button type="primary" @click="showModal">Open Modal with async logic<​/a-button>    <​a-modal      title="Title"      v-model:visible="visible"      :confirm-loading="confirmLoading"      @ok="handleOk"    >      <​p>{{ modalText }}<​/p>    <​/a-modal>  <​/div><​/template><​script lang="ts">import { ref, defineComponent } from 'vue';export default defineComponent({  setup() {    const modalText = ref<​string>('Content of the modal');    const visible = ref<​boolean>(false);    const confirmLoading = ref<​boolean>(false);    const showModal = () => {      visible.value = true;    };    const handleOk = () => {      modalText.value = 'The modal will be closed after two seconds';      confirmLoading.value = true;      setTimeout(() => {        visible.value = false;        confirmLoading.value = false;      }, 2000);    };    return {      modalText,      visible,      confirmLoading,      showModal,      handleOk,    };  },});<​/script>

确认对话框

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