当前位置:首页 > VUE

vue手动实现弹窗

2026-01-07 01:47:24VUE

实现弹窗组件的基本结构

在Vue中手动实现弹窗组件,需要创建一个独立的弹窗组件,并通过props控制其显示与隐藏。弹窗组件通常包含遮罩层、内容区域和关闭按钮。

<template>
  <div class="modal-mask" v-show="visible" @click.self="close">
    <div class="modal-container">
      <div class="modal-header">
        <h3>{{ title }}</h3>
        <button class="modal-close" @click="close">&times;</button>
      </div>
      <div class="modal-body">
        <slot></slot>
      </div>
      <div class="modal-footer">
        <button @click="close">取消</button>
        <button @click="confirm">确认</button>
      </div>
    </div>
  </div>
</template>

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

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

.modal-container {
  background: white;
  border-radius: 4px;
  width: 400px;
  padding: 20px;
}

.modal-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 15px;
}

.modal-close {
  background: none;
  border: none;
  font-size: 20px;
  cursor: pointer;
}

.modal-footer {
  margin-top: 15px;
  text-align: right;
}

.modal-footer button {
  margin-left: 10px;
  padding: 5px 10px;
}
</style>

使用弹窗组件

在父组件中引入并使用弹窗组件,通过v-model控制弹窗的显示状态。

<template>
  <div>
    <button @click="showModal = true">打开弹窗</button>
    <modal-dialog v-model:visible="showModal" @confirm="handleConfirm">
      <p>这里是弹窗内容</p>
    </modal-dialog>
  </div>
</template>

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

export default {
  components: {
    ModalDialog
  },
  data() {
    return {
      showModal: false
    }
  },
  methods: {
    handleConfirm() {
      console.log('用户点击了确认')
    }
  }
}
</script>

实现动画效果

为弹窗添加过渡动画可以提升用户体验,使用Vue的transition组件实现。

<template>
  <transition name="modal">
    <div class="modal-mask" v-show="visible" @click.self="close">
      <!-- 弹窗内容保持不变 -->
    </div>
  </transition>
</template>

<style scoped>
.modal-enter-active, .modal-leave-active {
  transition: opacity 0.3s ease;
}

.modal-enter-from, .modal-leave-to {
  opacity: 0;
}

.modal-enter-active .modal-container,
.modal-leave-active .modal-container {
  transition: transform 0.3s ease;
}

.modal-enter-from .modal-container,
.modal-leave-to .modal-container {
  transform: scale(0.9);
}
</style>

全局弹窗服务

对于需要在任何组件中都能调用的弹窗,可以创建全局弹窗服务。

// modalService.js
import Vue from 'vue'
import ModalDialog from './ModalDialog.vue'

const ModalConstructor = Vue.extend(ModalDialog)

const modalService = {
  open(options) {
    const instance = new ModalConstructor({
      propsData: options.props
    })

    if (options.onConfirm) {
      instance.$on('confirm', options.onConfirm)
    }

    instance.$mount()
    document.body.appendChild(instance.$el)
    instance.visible = true

    return {
      close: () => {
        instance.visible = false
        setTimeout(() => {
          document.body.removeChild(instance.$el)
          instance.$destroy()
        }, 300)
      }
    }
  }
}

export default modalService

在main.js中注册全局方法:

import modalService from './modalService'

Vue.prototype.$modal = modalService

在任何组件中使用:

vue手动实现弹窗

this.$modal.open({
  props: {
    title: '全局弹窗'
  },
  onConfirm: () => {
    console.log('全局弹窗确认')
  }
})

标签: vue
分享给朋友:

相关文章

vue原型实现

vue原型实现

Vue 原型实现的关键点 Vue.js 的原型系统是其响应式机制的核心部分,通过原型链实现数据的动态绑定和依赖收集。以下是实现 Vue 原型的主要技术要点。 数据响应式原理 Vue 通过 Objec…

vue功能实现

vue功能实现

Vue 功能实现指南 Vue.js 是一个渐进式 JavaScript 框架,广泛用于构建用户界面。以下是 Vue 功能的常见实现方法。 数据绑定 Vue 的核心功能之一是数据绑定,通过 v-mod…

通过vue实现

通过vue实现

Vue 实现方法 安装 Vue 确保已安装 Node.js 和 npm,通过以下命令安装 Vue CLI: npm install -g @vue/cli 创建 Vue 项目 使用 Vue CLI…

vue实现active

vue实现active

Vue 实现 active 状态的方法 在 Vue 中实现 active 状态可以通过以下几种方式实现,具体取决于使用场景和需求。 使用 v-bind:class 动态绑定类名 通过 v-bind:…

vue 实现视频

vue 实现视频

Vue 实现视频播放功能 使用 Vue 实现视频播放功能可以通过 HTML5 的 <video> 标签或第三方库(如 video.js)来实现。以下是两种常见的方法: 使用 HTML5…

vue 实现href

vue 实现href

Vue 实现 href 的方法 在 Vue 中实现 href 功能可以通过多种方式,具体取决于需求场景。以下是几种常见的方法: 使用 <a> 标签 直接使用 HTML 的 <a&g…