当前位置:首页 > VUE

vue手动实现dialog

2026-02-10 19:10:54VUE

手动实现 Vue Dialog 组件

在 Vue 中手动实现 Dialog 组件可以通过组合式 API 或选项式 API 完成,以下是核心实现思路和代码示例:

组件基础结构

创建 Dialog.vue 文件,包含模板、样式和逻辑:

<template>
  <div class="dialog-overlay" v-if="visible" @click.self="handleClose">
    <div class="dialog-container">
      <div class="dialog-header">
        <slot name="header">
          <h3>{{ title }}</h3>
        </slot>
        <button class="close-btn" @click="handleClose">×</button>
      </div>
      <div class="dialog-body">
        <slot></slot>
      </div>
      <div class="dialog-footer">
        <slot name="footer">
          <button @click="handleClose">取消</button>
          <button @click="handleConfirm">确认</button>
        </slot>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  props: {
    visible: {
      type: Boolean,
      default: false
    },
    title: {
      type: String,
      default: '提示'
    }
  },
  emits: ['update:visible', 'confirm', 'close'],
  methods: {
    handleClose() {
      this.$emit('update:visible', false);
      this.$emit('close');
    },
    handleConfirm() {
      this.$emit('confirm');
      this.handleClose();
    }
  }
};
</script>

<style scoped>
.dialog-overlay {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: rgba(0, 0, 0, 0.5);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 1000;
}

.dialog-container {
  background: white;
  border-radius: 4px;
  min-width: 300px;
  max-width: 80%;
}

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

.dialog-body {
  padding: 16px;
}

.dialog-footer {
  padding: 16px;
  border-top: 1px solid #eee;
  text-align: right;
}

.close-btn {
  background: none;
  border: none;
  font-size: 20px;
  cursor: pointer;
}
</style>

使用组件

在父组件中通过 v-model 控制 Dialog 显示/隐藏:

vue手动实现dialog

<template>
  <button @click="showDialog = true">打开弹窗</button>
  <Dialog v-model:visible="showDialog" @confirm="handleConfirm">
    <template #header>
      自定义标题
    </template>
    这里是弹窗内容
    <template #footer>
      <button @click="showDialog = false">自定义按钮</button>
    </template>
  </Dialog>
</template>

<script>
import Dialog from './Dialog.vue';

export default {
  components: { Dialog },
  data() {
    return {
      showDialog: false
    };
  },
  methods: {
    handleConfirm() {
      console.log('确认操作');
    }
  }
};
</script>

进阶功能扩展

动画效果 添加过渡动画增强用户体验:

<template>
  <transition name="fade">
    <div class="dialog-overlay" v-if="visible" @click.self="handleClose">
      <transition name="scale">
        <div class="dialog-container">
          <!-- 内容不变 -->
        </div>
      </transition>
    </div>
  </transition>
</template>

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

.scale-enter-active, .scale-leave-active {
  transition: transform 0.3s;
}
.scale-enter-from, .scale-leave-to {
  transform: scale(0.9);
}
</style>

全局调用 通过 Vue 插件方式实现全局调用:

vue手动实现dialog

// dialogPlugin.js
import Dialog from './Dialog.vue';

export default {
  install(app) {
    const showDialog = (options) => {
      const mountNode = document.createElement('div');
      document.body.appendChild(mountNode);

      const instance = createApp({
        render() {
          return h(Dialog, {
            visible: true,
            ...options,
            onClose: () => {
              instance.unmount();
              document.body.removeChild(mountNode);
              options.onClose?.();
            }
          });
        }
      });

      instance.mount(mountNode);
    };

    app.config.globalProperties.$dialog = showDialog;
  }
};

在 main.js 中安装插件:

import dialogPlugin from './dialogPlugin';
app.use(dialogPlugin);

组件内使用:

this.$dialog({
  title: '全局弹窗',
  content: '通过插件调用',
  onConfirm: () => console.log('全局确认')
});

标签: vuedialog
分享给朋友:

相关文章

vue手写签名如何实现

vue手写签名如何实现

实现手写签名的基本思路 在Vue中实现手写签名功能,通常需要借助HTML5的Canvas API来捕获用户的绘制操作。核心流程包括初始化画布、监听触摸/鼠标事件、记录轨迹以及生成签名图片。 安装依赖…

vue实现打印二维码

vue实现打印二维码

安装二维码生成库 使用 qrcode 或 vue-qrcode 库生成二维码。通过 npm 安装: npm install qrcode vue-qrcode 生成二维码 在 Vue 组…

vue实现slider

vue实现slider

Vue 实现 Slider 组件 使用 Vue 实现 Slider 组件可以通过原生开发或借助第三方库。以下是两种常见方法: 原生实现 Slider 创建一个基础的 Slider 组件,通过 v-m…

vue实现气泡

vue实现气泡

Vue 实现气泡效果的方法 在 Vue 中实现气泡效果可以通过 CSS 动画、第三方库或自定义组件完成。以下是几种常见实现方式: 使用纯 CSS 和 Vue 过渡 通过 Vue 的过渡系统结合 CS…

vue实现fragment

vue实现fragment

Vue 实现 Fragment 的方法 在 Vue 中,Fragment 允许组件返回多个根节点而不需要包裹一个额外的 DOM 元素。以下是几种实现方式: 使用 Vue 3 的 <templa…

vue实现全选下载

vue实现全选下载

Vue实现全选下载功能 全选下载功能通常涉及前端文件列表的多选操作和批量下载逻辑。以下是基于Vue的实现方案: 数据准备与渲染 在Vue组件中定义文件列表数据和选中状态: data() { r…