Skip to content

Dialog 弹出框

继承van-dialog所有属性

弹出模态框,常用于消息提示、消息确认,或在当前页面内完成特定的交互操作。支持组件调用和函数调用两种方式。

函数调用

为了便于使用 Dialog,Green UI 提供了一系列辅助函数,通过辅助函数可以快速唤起全局的弹窗组件。

比如使用 showDialog 函数,调用后会直接在页面中渲染对应的弹出框。

消息提示

用于提示一些消息,默认只包含一个确认按钮。

消息确认

用于确认消息,默认包含确认和取消按钮。

圆角按钮风格

将 theme 选项设置为 round-button 可以展示圆角按钮风格的弹窗。

异步关闭

通过 beforeClose 属性可以传入一个回调函数,在弹窗关闭前进行特定操作。

使用 Dialog 组件

如果你需要在 Dialog 内嵌入组件或其他自定义内容,可以直接使用 Dialog 组件,并使用默认插槽进行定制。使用前需要通过 app.use 等方式注册组件。

API

方法

Vant 中导出了以下 Dialog 相关的辅助函数:

方法名说明参数返回值
showDialog展示消息提示弹窗,默认包含确认按钮options: DialogOptionsPromise<void>
showConfirmDialog展示消息确认弹窗,默认包含确认和取消按钮options: ConfirmDialogOptionsPromise<void>
closeDialog关闭弹窗--
setDefaultOptions修改默认配置,影响所有的 showDialog 调用options: DialogOptions-
resetDefaultOptions重置默认配置,影响所有的 showDialog 调用--

DialogOptions

调用 showDialog 等方法时,支持传入以下选项:

参数说明类型默认值
title弹窗标题string-
width弹窗宽度,单位为 pxnumber320
message弹窗内容string-
theme弹窗主题,可选值为 default round-buttonstringdefault
messageAlign弹窗内容对齐方式,可选值为 left center rightstringcenter
className自定义弹窗类名string-
showConfirmButton是否显示确认按钮booleantrue
showCancelButton是否显示取消按钮booleanfalse
confirmButtonText确认按钮文字string确认
confirmButtonColor确认按钮颜色string#ee0a24
confirmButtonDisabled确认按钮是否禁用booleanfalse
cancelButtonText取消按钮文字string取消
cancelButtonColor取消按钮颜色string#000
cancelButtonDisabled取消按钮是否禁用booleanfalse
overlay是否显示遮罩层booleantrue
overlayClass自定义遮罩层类名string-
overlayStyle自定义遮罩层样式CSSProperties-
lockScroll是否锁定背景滚动booleantrue
allowHtml是否允许渲染 HTML 内容booleanfalse
beforeClose关闭弹窗前的回调函数,返回 false 可阻止弹窗关闭`(action: 'confirm''cancel') => boolean
transition弹出动画效果,可选值为 fade slide-up slide-down slide-left slide-rightstringfade
teleport指定弹出位置,可选值为 body parent viewportstringbody

Props

参数说明类型默认值
v-model:show是否显示弹窗booleanfalse
title弹窗标题string-
width弹窗宽度,单位为 pxnumber320
message弹窗内容string-
theme弹窗主题,可选值为 default round-buttonstringdefault
message-align弹窗内容对齐方式,可选值为 left center rightstringcenter
show-confirm-button是否显示确认按钮booleantrue
show-cancel-button是否显示取消按钮booleanfalse
confirm-button-text确认按钮文字string确认
confirm-button-color确认按钮颜色string#ee0a24
confirm-button-disabled确认按钮是否禁用booleanfalse
cancel-button-text取消按钮文字string取消
cancel-button-color取消按钮颜色string#000
cancel-button-disabled取消按钮是否禁用booleanfalse
overlay是否显示遮罩层booleantrue
overlay-class自定义遮罩层类名string-
overlay-style自定义遮罩层样式CSSProperties-
lock-scroll是否锁定背景滚动booleantrue
allow-html是否允许渲染 HTML 内容booleanfalse
before-close关闭弹窗前的回调函数,返回 false 可阻止弹窗关闭`(action: 'confirm''cancel') => boolean
transition弹出动画效果,可选值为 fade slide-up slide-down slide-left slide-rightstringfade
teleport指定弹出位置,可选值为 body parent viewportstringbody

Dialog Events

事件名说明回调参数
confirm点击确认按钮时触发-
cancel点击取消按钮时触发-
open打开弹窗时触发-
close关闭弹窗时触发-
opened打开动画结束时触发-
closed关闭动画结束时触发-

Dialog Slots

名称说明
default弹窗内容
title弹窗标题
footer弹窗按钮组