Skip to content

Modal 对话框

模态对话框组件,用于显示重要信息或表单。

基础用法

vue
<script setup>
import { ref } from 'vue'
const visible = ref(false)
</script>

<template>
  <MButton @click="visible = true">打开对话框</MButton>
  
  <MModal v-model="visible" title="对话框标题">
    <p>这是对话框的内容。</p>
  </MModal>
</template>

不同尺寸

vue
<template>
  <MModal v-model="visible" title="小型对话框" size="small">
    <p>小型对话框内容</p>
  </MModal>
  
  <MModal v-model="visible" title="大型对话框" size="large">
    <p>大型对话框内容</p>
  </MModal>
  
  <MModal v-model="visible" title="全屏对话框" size="full">
    <p>全屏对话框内容</p>
  </MModal>
</template>

自定义宽度

vue
<template>
  <MModal v-model="visible" title="自定义宽度" custom-width="600px">
    <p>自定义宽度的对话框</p>
  </MModal>
</template>

自定义底部

vue
<template>
  <MModal v-model="visible" title="自定义底部">
    <p>对话框内容</p>
    
    <template #footer>
      <MButton @click="visible = false">取消</MButton>
      <MButton type="primary" @click="handleSave">保存</MButton>
      <MButton type="danger" @click="handleDelete">删除</MButton>
    </template>
  </MModal>
</template>

无底部

vue
<template>
  <MModal v-model="visible" title="无底部" :show-footer="false">
    <p>没有底部按钮的对话框</p>
  </MModal>
</template>

确认对话框

vue
<script setup>
const handleConfirm = () => {
  console.log('已确认!')
  visible.value = false
}
</script>

<template>
  <MModal 
    v-model="visible" 
    title="确认操作"
    @confirm="handleConfirm"
  >
    <p>确定要执行此操作吗?</p>
  </MModal>
</template>

Props 属性

属性名类型默认值说明
modelValueboolean-对话框可见性(v-model)
titlestring''对话框标题
size'small' | 'medium' | 'large' | 'full''medium'对话框尺寸
customWidthstring-自定义最大宽度
showClosebooleantrue显示关闭按钮
showFooterbooleantrue显示底部区域
showCancelbooleantrue显示取消按钮
showConfirmbooleantrue显示确认按钮
cancelTextstring'取消'取消按钮文本
confirmTextstring'确定'确认按钮文本
confirmDisabledbooleanfalse禁用确认按钮
closeOnClickModalbooleantrue点击遮罩关闭
closeOnPressEscapebooleantrue按 ESC 键关闭
contentClassstring''内容区域的自定义类名

Events 事件

事件名参数说明
update:modelValue(value: boolean)可见性变化时触发
confirm-点击确认按钮时触发
cancel-取消对话框时触发
open-对话框打开时触发
close-对话框关闭时触发

Slots 插槽

插槽名说明
default对话框主体内容
header自定义头部内容
footer自定义底部内容

Released under the MIT License.