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

组件调用方式

在父组件中使用基础弹窗:

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

全局弹窗服务

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

// 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 轮播组件实现方法 使用第三方库(如 Swiper) 安装 Swiper 库: npm install swiper 在 Vue 组件中引入 Swiper: <template…

jquery方法

jquery方法

jQuery 核心方法 $() 或 jQuery() 是核心选择器方法,用于获取 DOM 元素或创建 jQuery 对象。支持 CSS 选择器、DOM 元素或 HTML 字符串。 $('#id…

uniapp如何将底部导航组件化

uniapp如何将底部导航组件化

在 uniapp 中将底部导航组件化 将底部导航组件化可以提高代码复用性和维护性。以下是具体实现方法: 创建自定义底部导航组件 新建一个组件文件,例如 tab-bar.vue,放置在 comp…

vue实现组件

vue实现组件

Vue 实现组件的方法 Vue 中实现组件可以通过多种方式,包括全局注册、局部注册、单文件组件(SFC)等。以下是常见的实现方法。 全局注册组件 全局注册的组件可以在任何 Vue 实例或组件中使用…

vue组件实现vmodel

vue组件实现vmodel

Vue 组件实现 v-model 在 Vue 中,v-model 是双向数据绑定的语法糖,默认情况下绑定 value 属性并监听 input 事件。在自定义组件中实现 v-model 需要正确配置 p…

vue实现父子组件

vue实现父子组件

Vue 父子组件通信实现方式 1. 父组件向子组件传递数据(Props) 父组件通过 props 向子组件传递数据,子组件通过 props 接收数据。 父组件模板: <template&…