当前位置:首页 > VUE

vue实现弹窗组件

2026-03-09 00:14:56VUE

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>
.modal-mask {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background-color: rgba(0, 0, 0, 0.5);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 999;
}

.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>
.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 {
  animation: bounce-in 0.3s;
}

.modal-leave-active .modal-container {
  animation: bounce-in 0.3s reverse;
}

@keyframes bounce-in {
  0% {
    transform: scale(0.95);
  }
  50% {
    transform: scale(1.05);
  }
  100% {
    transform: scale(1);
  }
}
</style>

全局弹窗服务

创建可编程调用的全局弹窗:

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

export function showModal(options) {
  const div = document.createElement('div')
  document.body.appendChild(div)

  const app = createApp({
    render() {
      return h(Modal, {
        visible: true,
        onClose: () => {
          app.unmount()
          div.remove()
        },
        ...options
      })
    }
  })

  app.mount(div)
}

使用全局弹窗:

import { showModal } from './modalService'

showModal({
  title: '全局弹窗',
  content: '通过服务调用的弹窗',
  onConfirm() {
    console.log('确认操作')
  }
})

可复用配置选项

扩展弹窗组件支持更多配置:

vue实现弹窗组件

<script>
export default {
  props: {
    title: String,
    showClose: {
      type: Boolean,
      default: true
    },
    width: {
      type: String,
      default: '50%'
    },
    beforeClose: Function
  },
  methods: {
    close() {
      if (this.beforeClose) {
        this.beforeClose(() => {
          this.doClose()
        })
      } else {
        this.doClose()
      }
    },
    doClose() {
      this.$emit('update:visible', false)
    }
  }
}
</script>

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

相关文章

vue实现表格多行修改

vue实现表格多行修改

实现多行编辑表格 在Vue中实现表格的多行编辑功能,可以通过以下方法完成。这里以Element UI的表格组件为例,但思路适用于其他UI库或原生实现。 数据准备 定义表格数据和编辑状态管理变量:…

vue实现下拉刷新组件

vue实现下拉刷新组件

实现下拉刷新组件的核心思路 下拉刷新功能通常通过监听触摸事件、滚动位置和动画效果实现。Vue中可以利用自定义指令或封装组件完成,以下为两种常见实现方式。 基于自定义指令的实现 创建指令监听触摸事件,…

vue 实现

vue 实现

Vue 实现的基本步骤 安装 Vue.js 可以通过 npm 或 CDN 引入。使用 npm 安装适合大型项目,CDN 适合快速原型开发。 npm install vue 在 HTML 文件中引入…

vue el 实现

vue el 实现

Vue 中使用 Element UI (el) 的实现方法 Element UI 是一个基于 Vue 的组件库,提供丰富的 UI 组件,常用于快速构建企业级中后台产品。以下介绍 Vue 项目中集成和使…

vue 实现注册

vue 实现注册

Vue 实现注册功能 注册功能通常包括表单验证、数据提交和用户反馈。以下是实现注册功能的步骤: 1. 创建注册表单组件 在 Vue 项目中创建一个注册表单组件,包含用户名、邮箱、密码等输入字段。…

vue mvvm实现

vue mvvm实现

Vue MVVM 实现原理 Vue.js 是一个基于 MVVM(Model-View-ViewModel)模式的框架,其核心是通过数据绑定和响应式系统实现视图与数据的自动同步。以下是 Vue 实现 M…