Skip to content

Modal 模态框组件

弹出模态框,常用于消息提示、消息确认、在当前页面内完成特定的交互操作。

温馨提示

本项目参考了 uView-Plus 开源项目的组件开发方式,基于 Vue 3 和 TypeScript 实现了自定义组件。目前该组件仍处于测试阶段。
感谢 uView-Plus 开源项目及其团队成员的贡献,他们的组件开发思路为本项目提供了宝贵地参考。如果需要了解更多组件开发细节,可以参考uView-Plus的 modal组件 的代码实现。

平台差异说明

APP(vue)H5微信小程序

基本使用示例

html
<!-- 全局使用 -->
<hy-modal :show="true" title="通知"></hy-modal>
<!-- 单个组件引入 -->
<HyModal :show="true" title="通知"></HyModal>
ts
import { HyModal } from "hy-app"

点击遮罩关闭

  • 有时候我们不显示"关闭"按钮的时候,需要点击遮罩也可以关闭Modal,这时通过配置closeOnClickOverlaytrue即可

注意

关闭事件需要自行处理,只会在开启closeOnClickOverlay后点击遮罩层执行close回调

html
<template>
    <hy-modal :show="show" closeOnClickOverlay @close="show = false"></hy-modal>
</template>

<script setup>
    import {ref} from "vue";
    const show = ref(true);
</script>

控制模态框宽度

  • 可以通过设置width参数控制模态框的宽度,不支持百分比,可以数值,px,rpx单位
html
<template>
    <hy-modal :show="true" width="300px"></hy-modal>
</template>

缩放效果

  • 开启缩放效果,在打开和收起模态框的时候,会带有缩放效果,具体效果请见示例,此效果默认开启,通过zoom参数配置
html
<template>
    <hy-modal :show="true" :zoom="false"></hy-modal>
</template>

对调按钮

  • 通过配置buttonReverse来对调按钮位置
  • 通过配置showCancelButton来显示取消按钮
html
<template>
    <hy-modal :show="true" buttonReverse showCancelButton></hy-modal>
</template>

自定义插槽

html
<template>
    <view >
        <hy-modal :show="show"  :title="title" >
            <view class="slot-content">
                <rich-text :nodes="content"></rich-text>
            </view>
        </-modal>
        <up-button @click="show = true">打开</up-button>
    </view>
</template>

<script setup>
    import { ref } from 'vue';

    // 创建响应式数据  
    const show = ref(false);
    const title = ref('标题');
    const content = ref(`空山新雨后<br>天气晚来秋`);
</script>

API

参数说明类型默认值
show是否显示模态框booleanfalse
title标题内容string-
content模态框内容,如传入slot内容,则此参数无效string-
confirmText确认按钮的文字string确认
cancelText取消按钮的文字string取消
showConfirmButton是否显示确认按钮booleantrue
showCancelButton是否显示取消按钮booleanfalse
confirmColor确认按钮的颜色stringColorConfig.primary
cancelColor取消按钮的颜色string#606266
buttonReverse对调确认和取消的位置booleanfalse
zoom是否开启缩放模式booleantrue
asyncClose是否异步关闭,只对确定按钮有效,见上方说明booleanfalse
asyncCloseTip确定按钮异步关闭启用时,如果点击取消时的文案提示string-
asyncCancelClose是否异步关闭,只对取消按钮有效booleanfalse
closeOnClickOverlay是否允许点击遮罩关闭Modal(注意:关闭事件需要自行处理,只会在开启closeOnClickOverlay后点击遮罩层执行close回调)booleanfalse
negativeTop往上偏移的值,给一个负的margin-top,往上偏移,避免和键盘重合的情况,单位任意,数值则默认为px单位string | number0
widthmodal宽度,不支持百分比,可以数值,px,rpx单位string | number650rpx
confirmButtonShape确认按钮的样式,如设置,将不会显示取消按钮circle | square-
contentTextAlign文案对齐方式center|left|rightleft

Events

事件名说明回调参数
confirm点击确认按钮时触发-
cancel点击取消按钮时触发-
close点击遮罩关闭出发,closeOnClickOverlay为true有效-
cancelOnAsync异步操作进行中点击取消按钮触发-

Slots

插槽名说明接收值
default传入自定义内容,见上方说明-
confirmButton传入自定义按钮,用于在微信小程序弹窗通过按钮授权的场景-
00:46