Back to Vant Weapp

Dialog 弹出框

packages/dialog/README.md

1.11.711.2 KB
Original Source

Dialog 弹出框

介绍

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

引入

app.jsonindex.json中引入组件,详细介绍见快速上手

json
"usingComponents": {
  "van-dialog": "@vant/weapp/dialog/index"
}

代码演示

消息提示

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

html
<van-dialog id="van-dialog" />
javascript
import Dialog from '@vant/weapp/dialog/dialog';

Dialog.alert({
  title: '标题',
  message: '弹窗内容',
}).then(() => {
  // on close
});

Dialog.alert({
  message: '弹窗内容',
}).then(() => {
  // on close
});

消息确认

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

html
<van-dialog id="van-dialog" />
javascript
import Dialog from '@vant/weapp/dialog/dialog';

Dialog.confirm({
  title: '标题',
  message: '弹窗内容',
})
  .then(() => {
    // on confirm
  })
  .catch(() => {
    // on cancel
  });

圆角按钮风格

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

html
<van-dialog id="van-dialog" />
javascript
import Dialog from '@vant/weapp/dialog/dialog';

Dialog.alert({
  title: '标题',
  message: '弹窗内容',
  theme: 'round-button',
}).then(() => {
  // on close
});

Dialog.alert({
  message: '弹窗内容',
  theme: 'round-button',
}).then(() => {
  // on close
});

异步关闭

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

html
<van-dialog id="van-dialog" />
javascript
import Dialog from '@vant/weapp/dialog/dialog';

const beforeClose = (action) =>
  new Promise((resolve) => {
    setTimeout(() => {
      if (action === 'confirm') {
        resolve(true);
      } else {
        // 拦截取消操作
        resolve(false);
      }
    }, 1000);
  });

Dialog.confirm({
  title: '标题',
  message: '弹窗内容',
  beforeClose,
});

组件调用

如果需要在弹窗内嵌入组件或其他自定义内容,可以使用组件调用的方式。

html
<van-dialog
  use-slot
  title="标题"
  show="{{ show }}"
  show-cancel-button
  confirm-button-open-type="getUserInfo"
  bind:close="onClose"
  bind:getuserinfo="getUserInfo"
>
  <image src="https://img.yzcdn.cn/1.jpg" />
</van-dialog>
js
Page({
  data: {
    show: true,
  },

  getUserInfo(event) {
    console.log(event.detail);
  },

  onClose() {
    this.setData({ show: false });
  },
});

自定义样式

如果需要自定义样式,建议使用 custom-class 实现,不在推荐 className 属性(在自定义组件中使用并不会生效),使用方法如下

组件调用

html
<van-dialog
  title="标题"
  message="弹窗内容"
  show="{{ show }}"
  custom-class="my-custom-class"
/>

API 调用

html
<van-dialog id="van-dialog" custom-class="my-custom-class" />

API

方法

方法名参数返回值介绍
DialogoptionsPromise展示弹窗
Dialog.alertoptionsPromise展示消息提示弹窗
Dialog.confirmoptionsPromise展示消息确认弹窗
Dialog.setDefaultOptionsoptionsvoid修改默认配置,对所有 Dialog 生效
Dialog.resetDefaultOptions-void重置默认配置,对所有 Dialog 生效
Dialog.close-void关闭弹窗
Dialog.stopLoading-void停止按钮的加载状态

Options

通过函数调用 Dialog 时,支持传入以下选项:

参数说明类型默认值
title标题string-
width弹窗宽度,默认单位为pxstring | number320px
message文本内容,支持通过\n换行string-
messageAlign内容对齐方式,可选值为left rightstringcenter
theme样式风格,可选值为round-buttonstringdefault
zIndexz-index 层级number100
className自定义类名,dialog 在自定义组件内时无效,已废弃,请使用 custom-class 代替,将在 2.0.0 移除string''
customStyle自定义样式string''
selector自定义选择器stringvan-dialog
showConfirmButton是否展示确认按钮booleantrue
showCancelButton是否展示取消按钮booleanfalse
confirmButtonText确认按钮的文案string确认
cancelButtonText取消按钮的文案string取消
overlay是否展示遮罩层booleantrue
overlayStyle自定义遮罩层样式object-
closeOnClickOverlay点击遮罩层时是否关闭弹窗booleanfalse
asyncClose已废弃,将在 2.0.0 移除,请使用 beforeClose 属性代替booleanfalse
beforeClose关闭前的回调函数,返回 false 可阻止关闭,支持返回 Promise(action) => boolean | Promise<boolean>-
context选择器的选择范围,可以传入自定义组件的 this 作为上下文object当前页面
transition动画名称,可选值为fade nonestringscale
confirmButtonOpenType确认按钮的微信开放能力,具体支持可参考 微信官方文档string-

OpenType Options

使用confirmButtonOpenType后,支持以下选项:

参数说明类型默认值open-type
appParameter打开 APP 时,向 APP 传递的参数string-launchApp
lang指定返回用户信息的语言,zh_CN 简体中文,
zh_TW 繁体中文,en 英文stringengetUserInfo
sessionFrom会话来源string-contact
businessId客服消息子商户 idnumber-contact
sendMessageTitle会话内消息卡片标题string当前标题contact
sendMessagePath会话内消息卡片点击跳转小程序路径string当前分享路径contact
sendMessageImgsendMessageImgstring截图contact
showMessageCard显示会话内消息卡片stringfalsecontact

Props

通过组件调用 Dialog 时,支持以下 Props:

参数说明类型默认值
show是否显示弹窗boolean-
confirm-button-id v1.10.25确认按钮的标识符,作为底层原生button组件的id值string-
title标题string-
width弹窗宽度,默认单位为pxstring | number320px
message文本内容,支持通过\n换行string-
theme样式风格,可选值为round-buttonstringdefault
message-align内容对齐方式,可选值为left rightstringcenter
z-indexz-index 层级number100
class-name自定义类名,dialog 在自定义组件内时无效,已废弃,请使用 custom-class 代替,将在 2.0.0 移除string''
custom-style自定义样式string''
show-confirm-button是否展示确认按钮booleantrue
show-cancel-button是否展示取消按钮booleanfalse
confirm-button-text确认按钮的文案string确认
cancel-button-text取消按钮的文案string取消
confirm-button-color确认按钮的字体颜色string#ee0a24
cancel-button-color取消按钮的字体颜色string#333
overlay是否展示遮罩层booleantrue
overlay-style v1.0.0自定义遮罩层样式object-
close-on-click-overlay点击遮罩层时是否关闭弹窗booleanfalse
use-slot是否使用自定义内容的插槽booleanfalse
use-title-slot是否使用自定义标题的插槽booleanfalse
use-confirm-button-slot 1.10.23是否使用自定义确认按钮的插槽booleanfalse
use-cancel-button-slot 1.10.23是否使用自定义取消按钮的插槽booleanfalse
async-close已废弃,将在 2.0.0 移除,请使用 beforeClose 属性代替booleanfalse
before-close关闭前的回调函数,返回 false 可阻止关闭,支持返回 Promise(action) => boolean | Promise<boolean>-
transition动画名称,可选值为fadestringscale
confirm-button-open-type确认按钮的微信开放能力,具体支持可参考 微信官方文档string-
root-portal v1.11.3是否从页面子树中脱离出来,用于解决各种 fixed 失效问题,微信基础库 >= 2.25.2 booleanfalse

OpenType Props

使用confirm-button-open-type后,支持以下 Props:

参数说明类型默认值open-type
app-parameter打开 APP 时,向 APP 传递的参数string-launchApp
lang指定返回用户信息的语言,zh_CN 简体中文,
zh_TW 繁体中文,en 英文stringengetUserInfo
session-from会话来源string-contact
business-id客服消息子商户 idnumber-contact
send-message-title会话内消息卡片标题string当前标题contact
send-message-path会话内消息卡片点击跳转小程序路径string当前分享路径contact
send-message-imgsendMessageImgstring截图contact
show-message-card显示会话内消息卡片stringfalsecontact

Events

事件说明回调参数
bind:close弹窗关闭时触发event.detail: 触发关闭事件的来源,
枚举为confirm,cancel,overlay
bind:confirm点击确认按钮时触发-
bind:cancel点击取消按钮时触发-
bind:getuserinfo点击确认按钮时,会返回获取到的用户信息,
从返回参数的 detail 中获取到的值同 wx.getUserInfo-
bind:contact客服消息回调-
bind:getphonenumber获取用户手机号回调-
bind:getrealtimephonenumber v1.10.21获取手机号实时验证回调,open-type=getRealtimePhoneNumber 时有效-
bind:agreeprivacyauthorization v1.10.25同意隐私协议回调,openType="agreePrivacyAuthorization"时有效-
bind:error当使用开放能力时,发生错误的回调-
bind:opensetting在打开授权设置页后回调-

Slot

名称说明
title自定义title显示内容,如果设置了title属性则不生效
confirm-button 1.10.23自定义confirm-button显示内容,需要 use-confirm-button-slottrue
cancel-button 1.10.23自定义cancel-button显示内容,需要 use-cancel-button-slottrue

外部样式类

类名说明
custom-class v1.10.8根节点样式类
cancle-button-class v1.10.21取消按钮样式类
confirm-button-class v1.10.21确认按钮样式类