当前位置:首页 > VUE

vue实现弹框组件

2026-02-23 00:47:18VUE

Vue 弹框组件实现方法

基础弹框组件结构

创建一个名为Modal.vue的文件,包含以下基础结构:

<template>
  <div class="modal-mask" v-show="visible" @click.self="close">
    <div class="modal-container">
      <div class="modal-header">
        <slot name="header"></slot>
      </div>
      <div class="modal-body">
        <slot></slot>
      </div>
      <div class="modal-footer">
        <slot name="footer"></slot>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  props: {
    visible: {
      type: Boolean,
      default: false
    }
  },
  methods: {
    close() {
      this.$emit('update:visible', false)
    }
  }
}
</script>

<style scoped>
.modal-mask {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: rgba(0,0,0,0.5);
  display: flex;
  justify-content: center;
  align-items: center;
}
.modal-container {
  background: white;
  padding: 20px;
  border-radius: 4px;
  min-width: 300px;
}
</style>

组件注册与使用

在父组件中注册并使用弹框组件:

vue实现弹框组件

<template>
  <button @click="showModal = true">打开弹框</button>
  <Modal v-model:visible="showModal">
    <template #header>
      <h3>标题</h3>
    </template>
    <p>这里是弹框内容</p>
    <template #footer>
      <button @click="showModal = false">关闭</button>
    </template>
  </Modal>
</template>

<script>
import Modal from './Modal.vue'

export default {
  components: { Modal },
  data() {
    return {
      showModal: false
    }
  }
}
</script>

动画效果实现

为弹框添加过渡动画效果:

<template>
  <transition name="modal">
    <div class="modal-mask" v-show="visible" @click.self="close">
      <!-- 内容保持不变 -->
    </div>
  </transition>
</template>

<style scoped>
.modal-enter-active,
.modal-leave-active {
  transition: opacity 0.3s ease;
}
.modal-enter-from,
.modal-leave-to {
  opacity: 0;
}
.modal-container {
  transition: transform 0.3s ease;
}
.modal-enter-active .modal-container,
.modal-leave-active .modal-container {
  transform: scale(1);
}
.modal-enter-from .modal-container,
.modal-leave-to .modal-container {
  transform: scale(0.9);
}
</style>

全局弹框服务

创建可编程式调用的弹框服务:

vue实现弹框组件

// modalService.js
import { createApp } from 'vue'

export default {
  install(app) {
    app.config.globalProperties.$modal = {
      show(options) {
        const modalApp = createApp({
          data() {
            return {
              visible: true,
              ...options
            }
          },
          template: `
            <Modal v-model:visible="visible">
              <template #header>
                <h3>{{ title }}</h3>
              </template>
              <div v-html="content"></div>
              <template #footer>
                <button @click="visible = false">关闭</button>
              </template>
            </Modal>
          `
        })
        modalApp.component('Modal', Modal)
        const mountNode = document.createElement('div')
        document.body.appendChild(mountNode)
        modalApp.mount(mountNode)
      }
    }
  }
}

在main.js中注册服务:

import modalService from './modalService.js'

const app = createApp(App)
app.use(modalService)
app.mount('#app')

使用方法:

this.$modal.show({
  title: '提示',
  content: '操作成功'
})

标签: 组件vue
分享给朋友:

相关文章

vue实现安全免登录

vue实现安全免登录

Vue 实现安全免登录的方案 免登录功能通常通过持久化认证状态(如 Token)实现,但需兼顾安全性。以下是基于 Vue 的常见实现方案: 基于 Token 的免登录 登录成功后存储 Token…

vue实现mvc

vue实现mvc

Vue 实现 MVC 模式 Vue 本身是一个 MVVM(Model-View-ViewModel)框架,但可以通过结构调整实现 MVC(Model-View-Controller)模式。以下是具体实…

vue实现递归

vue实现递归

Vue 实现递归组件的方法 在 Vue 中实现递归组件通常用于处理树形结构数据(如菜单、评论列表、文件目录等)。以下是具体实现方法: 定义递归组件 通过 name 选项让组件可以递归调用自身: &…

实现 vue 框架

实现 vue 框架

Vue 框架的基本实现 Vue 是一个渐进式 JavaScript 框架,用于构建用户界面。以下是实现 Vue 框架的基本步骤和核心概念。 核心概念 响应式数据绑定 Vue 的核心特性之一是响应式数…

computed vue 实现

computed vue 实现

computed 在 Vue 中的基本用法 Vue 的 computed 属性用于声明依赖于其他数据的计算属性。当依赖的数据变化时,计算属性会自动重新计算并缓存结果。 export defaul…

vue实现树

vue实现树

Vue 实现树形结构的方法 使用递归组件实现树形结构 递归组件是Vue中实现树形结构的常见方式。通过组件调用自身,可以轻松构建多层嵌套的树形数据。 <template> <ul…