当前位置:首页 > 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控制弹窗的显示状态。

vue手动实现弹窗

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

全局弹窗服务

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

vue手动实现弹窗

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

在任何组件中使用:

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

标签: vue
分享给朋友:

相关文章

vue实现主页

vue实现主页

Vue 实现主页的基本步骤 创建一个 Vue 主页通常涉及以下几个关键步骤,从项目初始化到页面布局和功能实现。 初始化 Vue 项目 使用 Vue CLI 或 Vite 初始化项目。Vue CLI…

vue实现闪烁

vue实现闪烁

Vue实现元素闪烁效果 使用CSS动画实现 通过Vue绑定class结合CSS动画实现闪烁效果,代码简洁且性能较好。 <template> <div :class="{…

vue实现socket

vue实现socket

Vue 中实现 WebSocket 通信 在 Vue 项目中实现 WebSocket 通信可以通过原生 WebSocket API 或第三方库(如 socket.io-client)完成。以下是两种常…

vue实现类别

vue实现类别

Vue 实现类别的几种方法 在 Vue 中实现类别功能可以通过多种方式完成,以下是一些常见的方法: 使用 v-bind:class 动态绑定类名 通过对象语法或数组语法动态绑定类名,根据条件添加或…

vue实现曲线

vue实现曲线

Vue 实现曲线的方法 在 Vue 中实现曲线可以通过多种方式,包括使用 SVG、Canvas 或第三方库如 D3.js、ECharts 等。以下是几种常见的方法: 使用 SVG 绘制曲线 SVG…

vue实现视频

vue实现视频

Vue 实现视频播放的方法 在 Vue 项目中实现视频播放可以通过多种方式完成,以下是几种常见的方法: 使用 HTML5 的 <video> 标签 在 Vue 组件中直接使用 HTML…