Modal对话框·续1
<template> <div> <a-button @click="showConfirm">Confirm</a-button> <a-button type="dashed" @click="showDeleteConfirm">Delete</a-button> <a-button type="dashed" @click="showPropsConfirm">With extra props</a-button> </div></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 = () => { Modal.confirm({ title: 'Do you Want to delete these items?', icon: createVNode(ExclamationCircleOutlined), content: createVNode('div', { style: 'color:red;' }, 'Some descriptions'), onOk() { console.log('OK'); }, onCancel() { console.log('Cancel'); }, class: 'test', }); }; const showDeleteConfirm = () => { Modal.confirm({ title: 'Are you sure delete this task?', icon: createVNode(ExclamationCircleOutlined), content: 'Some descriptions', okText: 'Yes', okType: 'danger', cancelText: 'No', onOk() { console.log('OK'); }, onCancel() { console.log('Cancel'); }, }); }; const showPropsConfirm = () => { Modal.confirm({ title: 'Are you sure delete this task?', icon: createVNode(ExclamationCircleOutlined), content: 'Some descriptions', okText: 'Yes', okType: 'danger', okButtonProps: { disabled: true, }, cancelText: 'No', onOk() { console.log('OK'); }, onCancel() { console.log('Cancel'); }, }); }; return { showConfirm, showDeleteConfirm, showPropsConfirm, }; },});</script>
国际化
设置 okText 与 cancelText 以自定义按钮文字。
<template> <div> <a-button type="primary" @click="showModal">Modal</a-button> <a-button @click="confirm">Confirm</a-button> <a-modal v-model:visible="visible" title="Modal" ok-text="确认" cancel-text="取消" @ok="hideModal" > <p>Bla bla ...</p> <p>Bla bla ...</p> <p>Bla bla ...</p> </a-modal> </div></template><script lang="ts">import { ExclamationCircleOutlined } from '@ant-design/icons-vue';import { defineComponent, ref, createVNode } from 'vue';import { Modal } from 'ant-design-vue';export default defineComponent({ setup() { const visible = ref<boolean>(false); const showModal = () => { visible.value = true; }; const hideModal = () => { visible.value = false; }; const confirm = () => { Modal.confirm({ title: 'Confirm', icon: createVNode(ExclamationCircleOutlined), content: 'Bla bla ...', okText: '确认', cancelText: '取消', }); }; return { visible, showModal, hideModal, confirm, }; },});</script>
自定义位置
使用 centered 或类似 style.top 的样式来设置对话框位置。
<template> <div id="components-modal-demo-position"> <a-button type="primary" @click="setModal1Visible(true)"> Display a modal dialog at 20px to Top </a-button> <a-modal title="20px to Top" style="top: 20px" v-model:visible="modal1Visible" @ok="setModal1Visible(false)" > <p>some contents...</p> <p>some contents...</p> <p>some contents...</p> </a-modal> <br /> <br /> <a-button type="primary" @click="modal2Visible = true"> Vertically centered modal dialog </a-button> <a-modal v-model:visible="modal2Visible" title="Vertically centered modal dialog" centered @ok="modal2Visible = false" > <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 modal1Visible = ref<boolean>(false); const modal2Visible = ref<boolean>(false); const setModal1Visible = (visible: boolean) => { modal1Visible.value = visible; }; return { modal1Visible, modal2Visible, setModal1Visible, }; },});</script>
Confirm
确认对话框(promise)
使用 confirm() 可以快捷地弹出确认框。onCancel/onOk 返回 promise 可以延迟关闭
<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 = () => { Modal.confirm({ title: 'Do you want to delete these items?', icon: createVNode(ExclamationCircleOutlined), content: 'When clicked the OK button, this dialog will be closed after 1 second', onOk() { return new Promise((resolve, reject) => { setTimeout(Math.random() > 0.5 ? resolve : reject, 1000); }).catch(() => console.log('Oops errors!')); }, // eslint-disable-next-line @typescript-eslint/no-empty-function onCancel() {}, }); }; return { showConfirm, }; },});</script>
自定义页脚按钮属性
传入 okButtonProps 和 cancelButtonProps 可分别自定义确定按钮和取消按钮的 props。
<template> <div> <a-button type="primary" @click="showModal">Open Modal with customized button props</a-button> <a-modal v-model:visible="visible" title="Basic Modal" :ok-button-props="{ disabled: true }" :cancel-button-props="{ disabled: true }" @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; }; const handleCancel = (e: MouseEvent) => { console.log(e); visible.value = false; }; return { visible, showModal, handleOk, handleCancel, }; },});</script>
API
| afterClose |
Modal 完全关闭后的回调 |
function |
无 |
|
|
|
|
|
|
| bodyStyle |
Modal body 样式 |
object |
{} |
|
|
|
|
|
|
| cancelText | 取消按钮文字 | string| slot | 取消 | |
|---|---|---|---|---|
| centered |
垂直居中展示 Modal |
Boolean |
false |
|
|
|
|
|
|
| closable |
是否显示右上角的关闭按钮 |
boolean |
true |
|
|
|
|
|
|
| closeIcon | 自定义关闭图标 | VNode | slot | - | |
|---|---|---|---|---|
| confirmLoading |
确定按钮 loading |
boolean |
无 |
|
|
|
|
|
|
| destroyOnClose |
关闭时销毁 Modal 里的子元素 |
boolean |
false |
|
|
|
|
|
|
| footer | 底部内容,当不需要默认底部按钮时,可以设为 :footer=”null” | string|slot | 确定取消按钮 | |
|---|---|---|---|---|
| forceRender |
强制渲染 Modal |
boolean |
false |
|
|
|
|
|
|
| getContainer |
指定 Modal 挂载的 HTML 节点 |
(instance): HTMLElement |
() => document.body |
|
|
|
|
|
|
| keyboard |
是否支持键盘 esc 关闭 |
boolean |
true |
|
|
|
|
|
|
| maskClosable |
点击蒙层是否允许关闭 |
boolean |
true |
|
|
|
|
|
|
| okText | 确认按钮文字 | string|slot | 确定 | |
|---|---|---|---|---|
| okType |
确认按钮类型 |
string |
primary |
|
|
|
|
|
|
| okButtonProps |
ok 按钮 props |
ButtonProps |
- |
|
|
|
|
|
|
| cancelButtonProps |
cancel 按钮 props |
ButtonProps |
- |
|
|
|
|
|
|
| title | 标题 | string|slot | 无 | |
|---|---|---|---|---|
| visible(v-model) |
对话框是否可见 |
boolean |
无 |
|
|
|
|
|
|
| width | 宽度 | string|number | 520 | |
|---|---|---|---|---|
| wrapClassName |
对话框外层容器的类名 |
string |
- |
|
|
|
|
|
|
| zIndex |
设置 Modal 的 z-index |
Number |
1000 |
|
|
|
|
|
|