当前位置:首页 > VUE

vue 实现弹窗组件

2026-01-16 21:57:52VUE

Vue 弹窗组件实现

基础弹窗组件结构

创建一个名为Modal.vue的文件,定义弹窗的基础结构和样式:

<template>
  <div class="modal-mask" v-show="visible" @click.self="close">
    <div class="modal-container">
      <div class="modal-header">
        <h3>{{ title }}</h3>
        <button @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: Boolean,
    title: String
  },
  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: rgba(0, 0, 0, 0.5);
  display: flex;
  justify-content: center;
  align-items: center;
}
.modal-container {
  background: white;
  min-width: 300px;
  border-radius: 4px;
  overflow: hidden;
}
.modal-header {
  padding: 16px;
  display: flex;
  justify-content: space-between;
  border-bottom: 1px solid #eee;
}
.modal-body {
  padding: 16px;
}
.modal-footer {
  padding: 16px;
  text-align: right;
  border-top: 1px solid #eee;
}
</style>

使用弹窗组件

在父组件中注册并使用弹窗:

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

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

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

进阶功能实现

动态控制弹窗宽度 在Modal组件中添加width属性:

vue 实现弹窗组件

<script>
export default {
  props: {
    width: {
      type: String,
      default: 'auto'
    }
  }
}
</script>

<style scoped>
.modal-container {
  width: v-bind(width);
}
</style>

添加动画效果 使用Vue的transition组件实现淡入淡出效果:

<template>
  <transition name="fade">
    <div class="modal-mask" v-show="visible" @click.self="close">
      <!-- 其余内容不变 -->
    </div>
  </transition>
</template>

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

全局弹窗服务 创建全局弹窗服务避免重复导入:

vue 实现弹窗组件

// modalService.js
import { createApp } from 'vue'
import Modal from './Modal.vue'

export default {
  install(app) {
    const modalInstance = createApp(Modal).mount(document.createElement('div'))
    document.body.appendChild(modalInstance.$el)

    app.config.globalProperties.$modal = {
      show(options) {
        Object.assign(modalInstance, options)
        modalInstance.visible = true
        return new Promise(resolve => {
          modalInstance.$once('confirm', () => resolve(true))
          modalInstance.$once('update:visible', val => !val && resolve(false))
        })
      }
    }
  }
}

在main.js中注册:

import modalService from './modalService.js'
const app = createApp(App)
app.use(modalService)

使用全局弹窗:

this.$modal.show({
  title: '全局弹窗',
  content: '这是全局弹窗内容'
}).then(confirmed => {
  console.log(confirmed ? '用户确认' : '用户取消')
})

标签: 组件vue
分享给朋友:

相关文章

vue 实现手册

vue 实现手册

以下是关于 Vue.js 实现手册的整理内容,涵盖核心概念、实践方法和常见场景的解决方案: 核心概念与基础用法 Vue.js 是一个渐进式 JavaScript 框架,核心特点包括数据驱动视图和组件…

vue表单实现搜索

vue表单实现搜索

实现Vue表单搜索功能 在Vue中实现表单搜索功能通常涉及表单绑定、事件处理和搜索逻辑。以下是具体实现方法: 数据绑定与表单结构 使用v-model实现表单与Vue数据的双向绑定: <t…

vue 移动端实现签名

vue 移动端实现签名

Vue 移动端签名实现方法 在移动端实现签名功能,可以通过HTML5的Canvas结合触摸事件完成。以下是具体实现步骤: 使用Canvas绘制签名 安装vue-signature-pad插件或直…

vue播放倍速怎么实现

vue播放倍速怎么实现

实现Vue播放倍速的方法 在Vue中实现播放倍速功能通常涉及HTML5的<video>元素或第三方播放器库。以下是几种常见实现方式: 使用原生HTML5 video元素 通过ref获取v…

vue实现收起展开面板

vue实现收起展开面板

实现收起展开面板的方法 在Vue中实现收起展开面板功能可以通过多种方式完成,以下是几种常见的实现方法: 使用v-show或v-if控制显示隐藏 利用Vue的指令可以轻松实现面板的展开和收起:…

vue实现导航栏切图

vue实现导航栏切图

Vue 实现导航栏切图 使用 Vue Router 实现基础导航 在 Vue 项目中,可以通过 Vue Router 实现导航栏的路由切换。创建一个导航栏组件,并在其中使用 <router-li…