当前位置:首页 > 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属性:

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

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

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

使用全局弹窗:

vue 实现弹窗组件

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

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

相关文章

vue设计与实现 书

vue设计与实现 书

vue设计与实现 书 《Vue.js设计与实现》是一本深入探讨Vue.js框架内部原理和设计思想的书籍,适合希望深入理解Vue.js的开发者。以下是关于这本书的详细信息: 书籍内容 《Vue…

vue实现alert

vue实现alert

实现自定义 Alert 组件 在 Vue 中可以通过组件化方式实现自定义 Alert 弹窗。以下是基于 Vue 3 的实现示例: 组件代码 (Alert.vue) <template>…

vue实现报表

vue实现报表

vue实现报表的方法 使用Vue实现报表通常需要结合图表库或表格组件。以下是几种常见方法: 使用ECharts 安装ECharts库: npm install echarts vue-echart…

vue实现新闻

vue实现新闻

Vue 实现新闻功能 使用 Vue 实现新闻功能通常涉及新闻列表展示、新闻详情页、分类筛选等模块。以下是实现的基本思路和代码示例。 新闻列表展示 通过 Vue 组件展示新闻列表,通常使用 v-for…

vue实现异步

vue实现异步

Vue 实现异步操作的常见方法 Vue 提供了多种方式来处理异步操作,包括数据获取、事件处理等场景。 使用 async/await 在 Vue 方法中可以直接使用 async/await 语法处理异…

vue实现素材

vue实现素材

Vue 实现素材的方法 使用 Vue 组件管理素材 在 Vue 项目中,可以通过组件化的方式管理素材。创建一个专门的组件来加载和显示素材,例如图片、视频或音频文件。组件可以接收素材的路径或 URL 作…