使用 Vue3 + Composition API 开发一个可复用的模态弹窗组件(附完整代码)

在日常开发中,弹窗是 UI 交互中非常常见的组件,本文将带你从 0 构建一个 可复用、支持多种内容、支持嵌套与异步处理的模态弹窗组件,并基于 Vue 3 的 Composition API 实现代码清晰、功能强大且易于维护的结构。


📦 技术栈说明

  • Vue 3.x
  • Composition API
  • <Teleport /> 组件传送内容至 body
  • 动画:CSS 动画或第三方库(如 animate.css)

🧱 组件目标功能

  • ✅ 基础弹窗显示/隐藏
  • ✅ 支持自定义内容插槽
  • ✅ 支持关闭事件回调
  • ✅ 支持遮罩层点击关闭
  • ✅ 支持外部控制 & 全局调用

🗂 目录结构建议

css 复制代码
css
复制编辑
src/
└── components/
    └── Modal.vue
    └── useModal.js

🔧 Modal.vue 组件实现

xml 复制代码
vue
复制编辑
<template>
  <Teleport to="body">
    <div v-if="visible" class="modal-mask" @click.self="handleClose">
      <div class="modal-container">
        <slot />
        <button class="close-btn" @click="handleClose">×</button>
      </div>
    </div>
  </Teleport>
</template>

<script setup>
import { defineProps, defineEmits, watch } from 'vue'

const props = defineProps({
  modelValue: Boolean
})

const emit = defineEmits(['update:modelValue', 'close'])

const visible = $computed(() => props.modelValue)

function handleClose() {
  emit('update:modelValue', false)
  emit('close')
}
</script>

<style scoped>
.modal-mask {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: rgba(0,0,0,0.5);
  display: flex;
  justify-content: center;
  align-items: center;
}
.modal-container {
  background: white;
  padding: 20px;
  border-radius: 8px;
  position: relative;
}
.close-btn {
  position: absolute;
  top: 5px;
  right: 10px;
  background: none;
  border: none;
  font-size: 18px;
}
</style>

🌱 使用方式

xml 复制代码
vue
复制编辑
<template>
  <button @click="show = true">打开弹窗</button>
  <Modal v-model="show" @close="onClose">
    <h3>自定义内容</h3>
    <p>这是弹窗的内容区</p>
  </Modal>
</template>

<script setup>
import { ref } from 'vue'
import Modal from '@/components/Modal.vue'

const show = ref(false)
const onClose = () => {
  console.log('弹窗关闭了')
}
</script>

🧩 useModal:组合式 API 封装调用

arduino 复制代码
js
复制编辑
import { ref } from 'vue'

export function useModal() {
  const isVisible = ref(false)

  const open = () => { isVisible.value = true }
  const close = () => { isVisible.value = false }

  return {
    isVisible,
    open,
    close
  }
}

使用示例:

xml 复制代码
vue
复制编辑
<script setup>
import { useModal } from '@/components/useModal'
import Modal from '@/components/Modal.vue'

const { isVisible, open, close } = useModal()
</script>

<template>
  <button @click="open">全局弹窗</button>
  <Modal v-model="isVisible" @close="close">
    <p>全局控制的弹窗</p>
  </Modal>
</template>

💡 提升建议

  • 添加过渡动画 transition
  • 支持键盘 ESC 关闭
  • 支持嵌套弹窗管理
  • 异步确认(如:确认删除操作)

✅ 总结

通过本文,你可以掌握如何在 Vue3 项目中使用 <Teleport>Composition API 构建一个功能完善、可复用、灵活扩展的弹窗组件,不仅适用于小型组件库,也适合企业项目中的实际使用。

相关推荐
葫芦和十三3 小时前
图解 MongoDB 05|文档模型设计:内嵌 vs 引用,反范式不是免费午餐
后端·mongodb·agent
不能放弃治疗6 小时前
单 Agent 实现模式
后端
IT_陈寒8 小时前
Redis内存爆了,原来我漏掉了这个致命配置
前端·人工智能·后端
fliter9 小时前
最后一块拼图:用 bitvec 构造 IPv4 包,真正做出自己的 Ping
后端
fliter10 小时前
用 Rust 解析并生成 ICMP 包:checksum、nom 与 cookie-factory
后端
蝎子莱莱爱打怪10 小时前
XZLL-IM干货系列 03|消息 ID 设计:一个 UUID 搞不定的事,我用两个 ID 解决了
后端·面试·开源
fliter10 小时前
从 panic 到 Result:用 Rust 重新整理一个 ping 项目的错误处理
后端
森蓝情丶11 小时前
我给 AI 搭了个法庭:一个前端仔的 LangGraph 实战全记录
前端·后端
JensCS猿11 小时前
从 Spring Boot 回看 SSM 框架:手动挡与自动挡的驾驶哲学
后端
爱勇宝11 小时前
干了近 8 年,一夜之间被裁:AI 时代,程序员最该害怕的不是 AI
前端·后端·程序员