当前位置:首页 > 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>

组件注册与使用

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

<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>

全局弹框服务

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

// 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实现组件循环

Vue 组件循环的实现方法 在 Vue 中实现组件循环通常使用 v-for 指令,可以循环渲染数组或对象的数据到组件上。 基本数组循环 通过 v-for 指令遍历数组,渲染多个相同结构的组件。…

vue实现公式

vue实现公式

在Vue中实现公式展示或计算可以通过多种方式实现,以下为具体方法: 使用模板插值与计算属性 若公式较简单,可直接在模板中使用插值表达式或计算属性。例如计算圆的面积: <template&…

vue实现键盘

vue实现键盘

Vue 实现键盘功能 在Vue中实现键盘功能通常涉及监听键盘事件、处理输入以及渲染虚拟键盘界面。以下是几种常见的实现方式: 监听键盘事件 通过@keydown或@keyup指令监听键盘事件,适用于…

vue 实现过滤

vue 实现过滤

Vue 实现过滤的方法 在 Vue 中实现过滤功能可以通过多种方式完成,包括使用计算属性、自定义过滤器、第三方库等。以下是几种常见的实现方法: 使用计算属性进行过滤 计算属性是 Vue 中最常用的…

vue实现导出

vue实现导出

Vue 实现导出功能的方法 在 Vue 项目中实现导出功能通常涉及将数据导出为 Excel、CSV 或其他格式。以下是几种常见的实现方式: 使用 xlsx 库导出 Excel 安装 xlsx 库:…

vue实现mouseout

vue实现mouseout

Vue 实现 mouseout 事件的方法 在 Vue 中实现 mouseout 事件可以通过多种方式完成,以下是几种常见的方法: 使用 v-on 指令 Vue 提供了 v-on 指令(或简写为…