当前位置:首页 > VUE

用vue实现弹窗

2026-03-10 08:55:14VUE

使用Vue实现弹窗

基础弹窗组件实现

创建一个名为Modal.vue的组件文件,包含模板、样式和逻辑:

<template>
  <div class="modal-mask" v-if="show" @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" v-if="showConfirm">确认</button>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  props: {
    show: Boolean,
    title: String,
    showConfirm: {
      type: Boolean,
      default: true
    }
  },
  methods: {
    close() {
      this.$emit('update:show', false);
    },
    confirm() {
      this.$emit('confirm');
    }
  }
};
</script>

<style>
.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: 8px;
  min-width: 300px;
  max-width: 80%;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.33);
}

.modal-header {
  display: flex;
  justify-content: space-between;
  padding: 15px;
  border-bottom: 1px solid #eee;
}

.modal-body {
  padding: 15px;
}

.modal-footer {
  padding: 15px;
  text-align: right;
  border-top: 1px solid #eee;
}
</style>

在父组件中使用弹窗

在需要弹窗的父组件中引入并使用:

<template>
  <div>
    <button @click="showModal = true">打开弹窗</button>
    <Modal 
      v-model:show="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('确认按钮被点击');
      this.showModal = false;
    }
  }
};
</script>

使用Vue插件方式全局注册

如需全局使用弹窗,可以创建插件:

// modalPlugin.js
import Modal from './Modal.vue';

export default {
  install(app) {
    app.component('Modal', Modal);
  }
};

// main.js
import { createApp } from 'vue';
import App from './App.vue';
import modalPlugin from './modalPlugin';

const app = createApp(App);
app.use(modalPlugin);
app.mount('#app');

动态控制弹窗内容

通过插槽和props实现内容动态化:

<Modal 
  v-model:show="showModal"
  :title="modalTitle"
  :show-confirm="hasConfirm"
>
  <template v-slot:default>
    <p>{{ modalContent }}</p>
    <input v-model="inputValue" />
  </template>
  <template v-slot:footer>
    <button @click="customAction">自定义按钮</button>
  </template>
</Modal>

动画效果实现

为弹窗添加过渡动画:

用vue实现弹窗

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

<style>
.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
分享给朋友:

相关文章

vue 实现

vue 实现

Vue 实现的基本步骤 安装 Vue.js 可以通过 npm 或 CDN 引入。使用 npm 安装适合大型项目,CDN 适合快速原型开发。 npm install vue 在 HTML 文件中引入…

vue实现钟表

vue实现钟表

使用 Vue 实现动态钟表 以下是一个基于 Vue 3 的动态钟表实现方案,包含时分秒指针和数字时间显示。 模板部分 <template> <div class="clock-…

vue实现点击

vue实现点击

实现点击事件的基本方法 在Vue中实现点击事件可以通过v-on指令或@简写方式绑定。以下是常见的点击事件绑定方法: <template> <button v-on:click=…

vue实现tablegrid

vue实现tablegrid

Vue 实现 TableGrid 的方法 使用 Element UI 的 Table 组件 Element UI 提供了强大的 Table 组件,可以快速实现表格布局。安装 Element UI 后,…

vue实现pie

vue实现pie

Vue 实现饼图 在 Vue 中实现饼图通常可以通过第三方图表库(如 ECharts、Chart.js 或 Highcharts)来完成。以下是使用 ECharts 和 Chart.js 的两种常见方…

vue实现表白

vue实现表白

Vue 实现表白页面 使用 Vue 可以快速创建一个动态、交互式的表白页面。以下是一个简单的实现方案: 基础结构 创建一个 Vue 项目或单文件组件,包含以下核心部分: <template&…