0
点赞
收藏
分享

微信扫一扫

Ant Design Vue V3 Modal 对话框

芭芭蘑菇 2022-02-07 阅读 373

模态对话框。

何时使用 #

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

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

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.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
appContext弹窗的上下文,一般用于获取全局注册组件、vuex 等内容--

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

const modal = Modal.info();

modal.update({
  title: '修改的标题',
  content: '修改的内容',
});

modal.destroy();
  • Modal.destroyAll

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

const router = new VueRouter({ ... })

// router change
router.beforeEach((to, from, next) => {
  Modal.destroyAll();
})

FAQ #

为什么 Modal 方法不能获取 全局注册组件、context、vuex 等内容和 ConfigProvider locale/prefixCls 配置, 以及不能响应式更新数据 ? #

直接调用 Modal 方法,组件会通过 Vue.render 动态创建新的 Vue 实体。其 context 与当前代码所在 context 并不相同,因而无法获取 context 信息。

当你需要 context 信息(例如使用全局注册的组件)时,可以通过 appContext 属性传递当前组件 context, 当你需要保留属性响应式时,你可以使用函数返回:

import { getCurrentInstance } from 'vue';

const appContext = getCurrentInstance().appContext;
const title = ref('some message');
Modal.confirm({
  title: () => title.value, // 此时 title 的改变,会同步更新 confirm 中的 title
  appContext,
});

 

<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>
import { defineComponent, ref } from 'vue';
export default defineComponent({
  setup() {
    const visible = ref(false);

    const showModal = () => {
      visible.value = true;
    };

    const handleOk = e => {
      console.log(e);
      visible.value = false;
    };

    return {
      visible,
      showModal,
      handleOk,
    };
  },

});
</script>

 

 

举报

相关推荐

0 条评论