Skip to content

Modal 模态框 ​

基于 mu-popup 的对话框组件,支持居中对话框与底部弹出、标题图标、确认/取消按钮、自定义内容插槽与遮罩行为。

小程序APPWeb
✓✓✓

实例 ​

以下按属性分别说明 mu-modal 的用法,可按需组合。

html
<template>
  <mu-modal :show="show" position="center" title="提示" content="确定要提交本次内容吗?"
    :show-confirm="true" @confirm="onConfirm" @close="show = false"></mu-modal>
</template>

只显示取消按钮 ​

showConfirm 默认为 false,不开启时对话框中只有取消按钮。

html
<mu-modal :show="show" position="center" title="提示" content="该提示仅提供取消按钮。"
  @close="show = false"></mu-modal>

按钮文字 ​

通过 confirmText 设置确定按钮文字;position 默认为 bottom,未指定时对话框从底部弹出。

html
<mu-modal :show="show" title="温馨提示" :show-confirm="true" confirm-text="我知道了"
  content="为保障你的账号安全,请勿向他人透露验证码。" @confirm="onConfirm" @close="show = false"></mu-modal>

按钮样式 ​

设置取消与确定按钮的文字、取消按钮文字颜色与确定按钮背景色。

html
<mu-modal :show="show" position="center" title="自定义按钮" content="自定义按钮文案与颜色。"
  cancel-text="返回" confirm-text="好的" cancel-color="var(--mu-text-secondary-color)"
  confirm-color="var(--mu-color-success)" :show-confirm="true"
  @confirm="onConfirm" @cancel="onCancel" @close="show = false"></mu-modal>

自定义内容 ​

通过默认插槽替换 content 文字,可放置任意组件。

html
<mu-modal :show="show" position="center" title="自定义内容" :show-confirm="true"
  @confirm="onConfirm" @close="show = false">
  <view class="modal-body">
    <text>这里是通过默认插槽自定义的内容区域,可放置任意组件。</text>
  </view>
</mu-modal>

点击遮罩关闭 ​

控制点击遮罩是否关闭对话框,设为 false 后只能通过按钮关闭。

html
<mu-modal :show="show" position="center" title="点击遮罩不关闭" content="试试点击遮罩区域。"
  :show-confirm="true" :close-on-click-mask="false" @confirm="onConfirm" @close="show = false"></mu-modal>

标题图标 ​

设置标题左侧的图标名称,取值为图标列表中的名称。

html
<mu-modal :show="show" position="center" icon="info-circle" title="提示" content="标题左侧显示对应图标。"
  :show-confirm="true" @confirm="onConfirm" @close="show = false"></mu-modal>

显示图标 ​

控制标题图标是否显示,默认为显示。

html
<mu-modal :show="show2" position="center" icon="info-circle" :show-icon="false" title="提示"
  content="showIcon 为 false 时图标不显示。" @close="show2 = false"></mu-modal>

宽度 ​

设置对话框宽度,为空时居中弹窗默认 560rpx。

html
<mu-modal :show="show" position="center" title="自定义宽度" content="width 与 radius 可自定义。"
  width="640rpx" :show-confirm="true" @confirm="onConfirm" @close="show = false"></mu-modal>

圆角 ​

设置对话框圆角,为空时居中和底部弹出默认 28rpx。

html
<mu-modal :show="show" position="center" title="自定义宽度" content="width 与 radius 可自定义。"
  radius="24rpx" :show-confirm="true" @confirm="onConfirm" @close="show = false"></mu-modal>

属性 ​

全部属性均可选,未传入时使用默认值;默认值列为 - 表示空字符串。

属性名称说明类型默认值
show是否显示booleanfalse
position弹出位置,可选 center、bottomstringbottom
title标题文字string-
content内容文字string-
icon标题左侧图标名称string-
showIcon是否显示标题图标booleantrue
iconSize标题图标尺寸number40
iconColor标题图标颜色stringvar(--mu-modal-icon-color, var(--mu-color-primary, #4C76FF))
showConfirm是否显示确定按钮booleanfalse
cancelText取消按钮文字string取消
confirmText确定按钮文字string确定
cancelColor取消按钮文字颜色stringvar(--mu-modal-cancel-color, var(--mu-text-secondary-color, #6B7280))
confirmColor确定按钮背景色stringvar(--mu-color-primary, #4C76FF)
width对话框宽度,为空时居中弹窗默认 560rpxstring-
radius对话框圆角,为空时 center/bottom 默认 28rpxstring-
mask是否显示遮罩booleantrue
maskBackground遮罩背景色stringrgba(0, 0, 0, 0.6)
closeOnClickMask点击遮罩是否关闭booleantrue
duration过渡动画时长(ms)number300
zIndex层级number999
customClass自定义根节点类名string-
customStyle自定义根节点样式string-

事件 ​

组件支持以下事件。

事件名说明回调参数
confirm点击确定按钮时触发-
cancel点击取消按钮时触发-
close关闭时触发(点击取消、遮罩或弹窗关闭)event: UniEvent

插槽 ​

组件支持以下插槽。

名称说明
default自定义内容区域,显示在 content 文字下方

注意事项 ​

  • 平台差异:App、小程序、Web 表现一致。
  • 使用注意:组件为受控模式,需通过 :show 控制显示,并在 @close 中把 show 置为 false。
  • 使用注意:点击确定按钮只触发 confirm,不会自动关闭,需自行在 @confirm 中关闭。
  • 使用注意:默认只有取消按钮(showConfirm 为 false),需要确定按钮时须显式开启。