For AI agents: the complete documentation index is available at https://docs.halo.run/llms.txt, the full documentation bundle is available at https://docs.halo.run/llms-full.txt, and this page is available as Markdown at https://docs.halo.run/developer-guide/plugin/api-reference/ui/base-components/modal.md.

VModal 弹窗

VModal 是一个模态对话框容器,可以承载表单、详情或确认内容。需要在插件中展示复杂内容时使用它。

引入

import { VModal } from "@halo-dev/components";

在 Halo Console 中,弹窗通常由 v-if 控制显隐,需要主动关闭时通过组件实例的 close() 方法完成。

示例

基础用法

<script lang="ts" setup>
import { ref } from "vue";
import { VButton, VModal } from "@halo-dev/components";

const modal = ref<InstanceType<typeof VModal> | null>(null);
const visible = ref(false);
</script>

<template>
  <VButton type="primary" @click="visible = true">打开弹窗</VButton>

  <VModal v-if="visible" ref="modal" title="编辑文章" @close="visible = false">
    这里是弹窗内容。
    <template #footer>
      <VButton type="primary" @click="modal?.close()">关闭</VButton>
    </template>
  </VModal>
</template>

带页脚操作

<script lang="ts" setup>
import { ref } from "vue";
import { VButton, VModal, VSpace } from "@halo-dev/components";

const modal = ref<InstanceType<typeof VModal> | null>(null);
const visible = ref(false);
</script>

<template>
  <VButton type="primary" @click="visible = true">打开弹窗</VButton>

  <VModal v-if="visible" ref="modal" title="发布文章" @close="visible = false">
    文章发布后将对所有访客可见。
    <template #footer>
      <VSpace>
        <VButton type="primary" @click="modal?.close()">确认发布</VButton>
        <VButton @click="modal?.close()">取消</VButton>
      </VSpace>
    </template>
  </VModal>
</template>

点击遮罩关闭

<script lang="ts" setup>
import { ref } from "vue";
import { VButton, VModal } from "@halo-dev/components";

const visible = ref(false);
</script>

<template>
  <VButton @click="visible = true">打开弹窗</VButton>

  <VModal
    v-if="visible"
    title="点击遮罩可关闭"
    layer-closable
    @close="visible = false"
  >
    也可以按 Esc 关闭。
  </VModal>
</template>

全屏

<script lang="ts" setup>
import { ref } from "vue";
import { VButton, VModal } from "@halo-dev/components";

const visible = ref(false);
</script>

<template>
  <VButton @click="visible = true">全屏打开</VButton>

  <VModal v-if="visible" title="全屏弹窗" fullscreen @close="visible = false">
    全屏弹窗适合放置代码编辑器或复杂表单。
  </VModal>
</template>

Props

属性类型默认值说明
visiblebooleanundefined是否显示,支持 v-model:visible;不传时组件挂载即显示
titlestringundefined标题
widthnumber500最大宽度,单位像素
heightstringundefined内容高度
fullscreenbooleanfalse是否全屏显示
bodyClassstring[]undefined应用到内容区域的 class 列表
mountToBodybooleanfalse是否通过 Teleport 挂载到 body
centeredbooleantrue是否在视口中垂直居中
layerClosablebooleanfalse是否允许点击遮罩关闭

Emits

事件参数说明
update:visibleboolean显示状态变化时触发
close-关闭时触发

Slots

插槽说明
default弹窗内容
header自定义整个头部区域
center头部中间区域
actions头部右侧操作区域(关闭按钮之前)
footer底部操作区域