当前位置:首页 > VUE

vue弹窗组件实现方法

2026-01-23 03:22:10VUE

基础弹窗组件实现

创建Modal.vue文件作为基础组件模板:

<template>
  <div class="modal-mask" v-if="visible" @click.self="close">
    <div class="modal-container">
      <slot name="header">
        <h3>{{ title }}</h3>
      </slot>
      <div class="modal-body">
        <slot></slot>
      </div>
      <div class="modal-footer">
        <slot name="footer">
          <button @click="close">关闭</button>
        </slot>
      </div>
    </div>
  </div>
</template>

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

<style>
.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" title="示例标题">
    <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-if="visible" @click.self="close">
      <div class="modal-container">
        <!-- 原有内容 -->
      </div>
    </div>
  </transition>
</template>

<style>
.modal-enter-active,
.modal-leave-active {
  transition: opacity 0.3s ease;
}
.modal-enter-from,
.modal-leave-to {
  opacity: 0;
}
</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" :title="title">
              {{ content }}
            </Modal>
          `
        })
        modalApp.component('Modal', Modal)
        const el = document.createElement('div')
        document.body.appendChild(el)
        modalApp.mount(el)
      }
    }
  }
}

在main.js中注册服务:

import modalService from './modalService'
app.use(modalService)

组件内调用方式:

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

标签: 组件方法
分享给朋友:

相关文章

vue实现穿梭框树组件

vue实现穿梭框树组件

Vue 穿梭框树组件实现 穿梭框树组件通常结合了树形结构和穿梭框功能,允许用户在左右两栏之间移动树节点数据。以下是基于 Vue 的实现方案: 核心功能设计 数据结构 树形数据通常采用嵌套结构,例如:…

uniapp倒计时组件

uniapp倒计时组件

uniapp倒计时组件实现方法 使用内置组件实现 uniapp提供了<countdown>组件用于倒计时功能,支持自定义格式和样式。示例代码如下: <countdown…

react组件如何通讯

react组件如何通讯

React 组件通讯方式 React 组件间的通讯方式主要包括以下几种方法,适用于不同场景下的数据传递和状态管理需求。 父子组件通讯(Props 传递) 父组件通过 props 向子组件传递数据或回…

vue动态组件实现

vue动态组件实现

动态组件的基本用法 在Vue中,动态组件通过<component>标签和is属性实现。is属性可以绑定组件名称或组件选项对象,实现动态切换。 <template> <…

vue实现秒表组件

vue实现秒表组件

实现秒表组件的基本思路 使用Vue实现秒表组件需要管理时间状态、控制计时器的启动/暂停/重置功能,并通过计算属性动态显示格式化时间。核心逻辑包括利用setInterval更新计时数据,并通过生命周期钩…

vue实现盖章组件

vue实现盖章组件

实现Vue盖章组件的步骤 组件结构设计 创建一个Vue组件,包含画布元素和必要的控制按钮。画布用于显示盖章效果,按钮用于触发盖章操作。组件需要接收图片和盖章位置等参数。 <template&g…