当前位置:首页 > 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 中实现组件可以通过多种方式,包括全局注册、局部注册、单文件组件(SFC)等。以下是常见的实现方法。 全局注册组件 全局注册的组件可以在任何 Vue 实例或组件中使用。…

vue VUE实现日历组件功能

vue VUE实现日历组件功能

Vue 日历组件实现方法 基础日历结构 使用Vue的模板语法构建日历的HTML结构,通常包含头部(年月显示、切换按钮)和日历表格(星期标题和日期格子)。 <template> <…

Vue组件实现方法

Vue组件实现方法

Vue组件的基本实现 Vue组件是Vue.js的核心概念之一,允许将UI拆分为独立可复用的代码片段。组件的实现方式有多种,以下是常见的几种方法。 单文件组件(SFC) 单文件组件以.vue为后缀,将…

vue实现水印组件

vue实现水印组件

Vue 水印组件的实现方法 使用Canvas绘制水印 在Vue组件中通过Canvas动态生成水印图案,将其作为背景添加到目标元素上。 <template> <div ref="…

vue实现组件切换

vue实现组件切换

Vue 组件切换的实现方法 在 Vue 中实现组件切换有多种方式,以下是几种常见的方法: 动态组件 使用 Vue 内置的 <component> 元素和 is 属性可以动态切换组件:…

vue 实现弹窗组件

vue 实现弹窗组件

Vue 弹窗组件实现 基础弹窗组件结构 创建一个名为Modal.vue的文件,定义弹窗的基础结构和样式: <template> <div class="modal-mask"…