当前位置:首页 > 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 中实现组件循环通常使用 v-for 指令,可以循环渲染数组或对象的数据到组件上。 基本数组循环 通过 v-for 指令遍历数组,渲染多个相同结构的组件。…

vue 实现长列表

vue 实现长列表

Vue 实现长列表的优化方案 虚拟滚动技术 虚拟滚动是处理长列表的高效方式,仅渲染可视区域内的元素。通过计算滚动位置动态加载和卸载DOM节点,大幅减少内存占用和渲染压力。 <template…

vue实现选择季度

vue实现选择季度

Vue 实现选择季度的几种方法 使用下拉选择框(Select) 在 Vue 中可以通过 v-model 绑定一个下拉选择框来实现季度选择。数据可以预先定义为一个包含季度选项的数组。 <…

vue的动画实现

vue的动画实现

Vue 动画实现方式 Vue 提供了多种方式实现动画效果,主要通过内置组件和第三方库支持。 使用 Vue 内置过渡组件 Vue 的 transition 和 transition-group 组件可…

vue 页签实现

vue 页签实现

Vue 页签实现方法 在 Vue 中实现页签功能通常涉及动态组件、路由或状态管理。以下是几种常见的实现方式: 使用动态组件 通过 Vue 的 <component> 动态组件结合 v-f…

vue页面分离的实现

vue页面分离的实现

Vue页面分离的实现方法 将Vue页面分离为多个组件或模块,有助于提升代码可维护性和复用性。以下是几种常见的实现方式: 组件化拆分 通过将页面拆分为多个子组件,每个组件负责特定功能或UI部分。使用i…