当前位置:首页 > VUE

vue实现简单的弹窗

2026-02-09 14:58:11VUE

使用 Vue 实现简单弹窗

组件实现

创建一个名为 Modal.vue 的组件文件,包含弹窗的基础结构和逻辑:

<template>
  <div class="modal-overlay" v-if="isOpen" @click.self="close">
    <div class="modal-content">
      <slot></slot>
      <button @click="close">关闭</button>
    </div>
  </div>
</template>

<script>
export default {
  props: {
    isOpen: {
      type: Boolean,
      required: true
    }
  },
  methods: {
    close() {
      this.$emit('close')
    }
  }
}
</script>

<style scoped>
.modal-overlay {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: rgba(0, 0, 0, 0.5);
  display: flex;
  justify-content: center;
  align-items: center;
}

.modal-content {
  background: white;
  padding: 20px;
  border-radius: 5px;
  max-width: 500px;
  width: 80%;
}
</style>

父组件调用

在需要使用弹窗的父组件中引入并使用:

vue实现简单的弹窗

<template>
  <div>
    <button @click="showModal = true">打开弹窗</button>
    <Modal :isOpen="showModal" @close="showModal = false">
      <h3>弹窗标题</h3>
      <p>这是弹窗内容</p>
    </Modal>
  </div>
</template>

<script>
import Modal from './Modal.vue'

export default {
  components: {
    Modal
  },
  data() {
    return {
      showModal: false
    }
  }
}
</script>

进阶功能

为弹窗添加动画效果,可以修改 Modal.vue

<template>
  <transition name="fade">
    <div class="modal-overlay" v-if="isOpen" @click.self="close">
      <transition name="slide">
        <div class="modal-content">
          <slot></slot>
          <button @click="close">关闭</button>
        </div>
      </transition>
    </div>
  </transition>
</template>

<style scoped>
.fade-enter-active, .fade-leave-active {
  transition: opacity 0.3s;
}
.fade-enter, .fade-leave-to {
  opacity: 0;
}

.slide-enter-active, .slide-leave-active {
  transition: transform 0.3s;
}
.slide-enter, .slide-leave-to {
  transform: translateY(-50px);
}
</style>

全局弹窗服务

创建一个全局弹窗服务,便于在任何组件中调用:

vue实现简单的弹窗

// modalService.js
import Vue from 'vue'

const ModalBus = new Vue()

export default {
  show(content, options = {}) {
    ModalBus.$emit('show', { content, ...options })
  },
  hide() {
    ModalBus.$emit('hide')
  },
  onShow(callback) {
    ModalBus.$on('show', callback)
  },
  onHide(callback) {
    ModalBus.$on('hide', callback)
  }
}

然后在根组件中注册全局弹窗容器:

<template>
  <div id="app">
    <GlobalModal />
    <router-view/>
  </div>
</template>

<script>
import GlobalModal from './components/GlobalModal.vue'

export default {
  components: { GlobalModal }
}
</script>

实现 GlobalModal.vue 组件:

<template>
  <Modal :isOpen="isOpen" @close="hide">
    <div v-html="content"></div>
  </Modal>
</template>

<script>
import Modal from './Modal.vue'
import modalService from '../services/modalService'

export default {
  components: { Modal },
  data() {
    return {
      isOpen: false,
      content: ''
    }
  },
  created() {
    modalService.onShow(({ content }) => {
      this.content = content
      this.isOpen = true
    })
    modalService.onHide(() => {
      this.isOpen = false
    })
  },
  methods: {
    hide() {
      modalService.hide()
    }
  }
}
</script>

这样可以在任何组件中通过 modalService.show()modalService.hide() 来控制弹窗的显示和隐藏。

标签: 简单vue
分享给朋友:

相关文章

vue实现https

vue实现https

Vue 中实现 HTTPS 的步骤 在 Vue 项目中实现 HTTPS 主要涉及开发环境配置和生产环境部署。以下是具体方法: 开发环境配置(Vue CLI 或 Vite) 使用 mkcert 生成…

vue实现缩放

vue实现缩放

Vue 实现缩放的方法 在 Vue 中实现缩放功能可以通过多种方式完成,以下是一些常见的方法: 使用 CSS transform 属性 通过 CSS 的 transform: scale() 属性可…

vue实现左右布局

vue实现左右布局

实现左右布局的方法 在Vue中实现左右布局可以通过多种方式完成,以下是几种常见的方法: 使用Flexbox布局 Flexbox是一种现代的CSS布局方式,可以轻松实现左右布局。 <templ…

vue如何实现滚动

vue如何实现滚动

Vue 实现滚动的方法 使用原生 JavaScript 方法 在 Vue 中可以通过 window.scrollTo 或 Element.scrollIntoView 实现滚动。例如,滚动到页面顶部:…

vue实现左右滑动

vue实现左右滑动

Vue 实现左右滑动效果 使用 touch 事件实现基础滑动 监听 touchstart、touchmove 和 touchend 事件实现基础滑动逻辑: <template> &l…

设计与实现vue

设计与实现vue

Vue 设计与实现概述 Vue 是一个渐进式 JavaScript 框架,用于构建用户界面。其核心设计理念包括响应式数据绑定、组件化开发和虚拟 DOM 渲染。以下是 Vue 设计与实现的关键点。 核…