当前位置:首页 > VUE

vue手动实现弹窗

2026-02-09 13:21:34VUE

Vue 手动实现弹窗组件

创建弹窗组件

components 目录下新建 Modal.vue 文件,定义弹窗结构和样式:

<template>
  <div class="modal" v-if="isVisible">
    <div class="modal-content">
      <span class="close" @click="close">×</span>
      <slot></slot>
    </div>
  </div>
</template>

<script>
export default {
  props: {
    isVisible: {
      type: Boolean,
      default: false
    }
  },
  methods: {
    close() {
      this.$emit('close');
    }
  }
};
</script>

<style scoped>
.modal {
  position: fixed;
  z-index: 1000;
  left: 0;
  top: 0;
  width: 100%;
  height: 100%;
  background-color: rgba(0, 0, 0, 0.5);
  display: flex;
  justify-content: center;
  align-items: center;
}

.modal-content {
  background-color: white;
  padding: 20px;
  border-radius: 5px;
  width: 50%;
  position: relative;
}

.close {
  position: absolute;
  right: 10px;
  top: 5px;
  font-size: 24px;
  cursor: pointer;
}
</style>

在父组件中使用弹窗

在需要弹窗的父组件中引入并控制弹窗显示:

vue手动实现弹窗

<template>
  <div>
    <button @click="showModal">打开弹窗</button>
    <Modal :isVisible="modalVisible" @close="hideModal">
      <h3>弹窗标题</h3>
      <p>这里是弹窗内容</p>
    </Modal>
  </div>
</template>

<script>
import Modal from '@/components/Modal.vue';

export default {
  components: {
    Modal
  },
  data() {
    return {
      modalVisible: false
    };
  },
  methods: {
    showModal() {
      this.modalVisible = true;
    },
    hideModal() {
      this.modalVisible = false;
    }
  }
};
</script>

扩展功能:动态内容传递

通过插槽和 props 实现更灵活的弹窗内容控制:

<!-- Modal.vue -->
<template>
  <div class="modal" v-if="isVisible">
    <div class="modal-content">
      <span class="close" @click="close">×</span>
      <h3>{{ title }}</h3>
      <slot name="content"></slot>
      <div class="footer">
        <slot name="footer"></slot>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  props: {
    isVisible: Boolean,
    title: String
  },
  methods: {
    close() {
      this.$emit('close');
    }
  }
};
</script>

使用动态弹窗

在父组件中传递不同内容:

vue手动实现弹窗

<template>
  <Modal 
    :isVisible="modalVisible" 
    :title="modalTitle"
    @close="hideModal"
  >
    <template v-slot:content>
      <p>自定义内容区域</p>
    </template>
    <template v-slot:footer>
      <button @click="submit">提交</button>
      <button @click="hideModal">取消</button>
    </template>
  </Modal>
</template>

<script>
export default {
  data() {
    return {
      modalVisible: false,
      modalTitle: '自定义标题'
    };
  }
};
</script>

全局弹窗服务

创建全局弹窗服务实现任意位置调用:

// services/modal.js
import Vue from 'vue';
import ModalComponent from '@/components/Modal.vue';

const ModalConstructor = Vue.extend(ModalComponent);

const Modal = (options) => {
  const instance = new ModalConstructor({
    propsData: options.props
  }).$mount();

  document.body.appendChild(instance.$el);

  return {
    show: () => {
      instance.isVisible = true;
    },
    hide: () => {
      instance.isVisible = false;
    }
  };
};

export default Modal;

全局调用示例

在任意组件中使用全局弹窗:

import Modal from '@/services/modal';

const modal = Modal({
  props: {
    title: '全局弹窗'
  }
});

modal.show();

// 关闭弹窗
modal.hide();

标签: vue
分享给朋友:

相关文章

vue实现drag

vue实现drag

Vue 实现拖拽功能的方法 在 Vue 中实现拖拽功能可以通过原生 HTML5 的拖拽 API 或第三方库如 vuedraggable 来完成。以下是两种常见方法的实现方式。 使用 HTML5 拖拽…

vue 实现列表

vue 实现列表

Vue 实现列表的方法 在 Vue 中实现列表通常使用 v-for 指令,结合数组或对象数据渲染动态列表。以下是几种常见的实现方式: 基础列表渲染 使用 v-for 指令遍历数组,渲染列表项。ite…

vue实现阻塞

vue实现阻塞

Vue 实现阻塞的方法 在 Vue 中实现阻塞操作通常涉及异步控制、状态管理或生命周期钩子的使用。以下是几种常见方法: 使用 async/await 处理异步阻塞 通过 async/await 可…

vue 实现loading

vue 实现loading

Vue 实现 Loading 的方法 使用 v-if 和 v-show 控制显示 在 Vue 中可以通过 v-if 或 v-show 控制 loading 组件的显示与隐藏。v-if 会动态创建或销毁…

vue实现首页

vue实现首页

Vue 实现首页的基本步骤 安装 Vue.js 和相关依赖,可以通过 Vue CLI 或 Vite 快速初始化项目。Vue CLI 适合传统项目,Vite 更适合现代前端开发。 npm instal…

vue实现点击跳转

vue实现点击跳转

Vue 实现点击跳转的方法 在 Vue 中实现点击跳转可以通过以下几种方式: 使用 router-link router-link 是 Vue Router 提供的组件,用于声明式导航。适合在模板中…