Skip to content

AtModal

沈孟平
最后更新于 2026-04-23 19:53:53

一个封装了全屏和可拖拽的模态框组件。

Tip

可以使用 useModal 来控制模态框的显示隐藏

组件定位

AtModal 是一个偏“命令式”的弹窗容器,而不是简单的 v-model:show 薄封装。它在 NModal 之上补了几件中后台场景很常用的能力:

  • 统一的头部、内容区和底部操作区
  • 可拖拽
  • 可切换全屏
  • 内置滚动区域,便于在长表单或详情页中使用
  • 支持从 AtConfigProvider 读取全局默认配置

推荐使用方式

  1. 通过 refuseModal 调用 open() / close()
  2. 如果弹窗内容里还要拿子组件实例,建议 await open() 之后再访问。
  3. 如果你要拦截关闭动作,请使用 on-before-close,返回 false 即可阻止关闭。

代码演示

基础示例

配合表单使用

注意,需要 await open() 之后才能获取到表单的实例。

API

TIP

AtModalProps 继承自 NaiveUI Modal Props

ModalProvider Props

名称类型默认值说明版本
content-clsstringundefined内容区样式v0.12.29
mask-closablebooleanfalse点击遮罩时是否发出 update:show 事件v0.12.29

AtModal Props

名称类型默认值说明版本
titlestringundefined标题
widthnumber | string800px模态框主体宽度
show-actionbooleantrue是否显示 action
confirm-loadingbooleanundefinedshowAction 为 true 时才有用
positive-btn-textstring'确认'确认按钮文案
negative-btn-textstring'取消'取消按钮文案
draggablebooleanfalse是否可以拖拽
fullscreenbooleantrue是否显示全屏按钮
default-fullscreenbooleanfalse打开时默认全屏
content-clsstringundefined内容区样式
on-before-closefunctionundefined关闭前调用,返回 false 则不关闭
on-confirmfunctionundefined点击确认按钮回调
on-cancelfunctionundefined点击取消按钮回调

AtModal Events

名称参数说明版本
confirm()点击确认按钮时触发
cancel()点击取消按钮或关闭模态框时触发

AtModal Slots

名称参数说明版本
default()模态框主体内容
header()自定义头部标题区域
header-extra()自定义头部右上角区域,默认是全屏按钮
left-action()底部操作区左侧内容
right-action()底部操作区右侧内容,默认是取消/确认按钮

AtModal Expose

名称类型说明版本
open() => Promise<void>打开模态框;如果后续要访问弹窗内部实例,建议 await open()
close() => void关闭模态框
scroll-refScrollbarInst用于在模态框内定位元素: 例如滚动时固定某个元素
content-max-heightstring用于外部获取内容可视区高度

changelog

89e72 - feat(components): 在全局配置添加modal配置项并合并属性 on 2025/9/19
31050 - fix(components): AtModal need header-extra slot on 2025/3/14
511b0 - feat(components): AtForm新增autoRequired自动添加rule功能,更新AtForm placeholder 错误调整 on 2025/1/14
96641 - fix(components): AtModal 移除距顶部 10vh 的距离 on 2024/10/15
36eee - fix(components): AtModal types on 2024/9/11
d8146 - feat(components): AtModal add left and right action on 2024/8/20
cdcbb - feat(docs): add AtModal on 2024/7/31
d4a4d - fix(components): 模态框忘记导入按钮等组件 on 2024/7/16

贡献者

Last updated: