当前位置:首页 > VUE

vue手动实现dialog

2026-01-14 02:31:16VUE

手动实现 Vue Dialog 组件

在 Vue 中手动实现一个 Dialog(对话框)组件,可以通过组合 Vue 的组件系统、插槽(slot)和自定义事件来完成。以下是实现步骤和代码示例:

创建 Dialog 组件

创建一个基本的 Dialog 组件,包含标题、内容和操作按钮。使用 v-model 控制显示/隐藏状态。

vue手动实现dialog

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

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

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

.dialog {
  background: white;
  border-radius: 4px;
  width: 400px;
  max-width: 90%;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
}

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

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

使用 Dialog 组件

在父组件中使用 Dialog,并通过 v-model 控制其显示/隐藏状态。

vue手动实现dialog

<template>
  <div>
    <button @click="showDialog = true">打开对话框</button>
    <Dialog v-model:visible="showDialog" title="自定义标题" @confirm="handleConfirm">
      <p>这是对话框的内容,可以通过插槽自定义。</p>
    </Dialog>
  </div>
</template>

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

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

动态控制 Dialog

可以通过方法动态控制 Dialog 的显示和隐藏,例如在异步操作完成后关闭 Dialog。

<template>
  <div>
    <button @click="showDialog = true">打开对话框</button>
    <Dialog v-model:visible="showDialog" @confirm="submitForm">
      <form @submit.prevent="submitForm">
        <input v-model="inputValue" placeholder="输入内容" />
      </form>
    </Dialog>
  </div>
</template>

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

export default {
  components: { Dialog },
  data() {
    return {
      showDialog: false,
      inputValue: ''
    };
  },
  methods: {
    submitForm() {
      if (this.inputValue) {
        console.log('提交的内容:', this.inputValue);
        this.showDialog = false;
      }
    }
  }
};
</script>

扩展功能

可以通过扩展 Dialog 组件,支持更多自定义功能,例如:

  • 自定义按钮文本
  • 异步加载内容
  • 动画效果
<template>
  <div class="dialog-overlay" v-if="visible" @click.self="close">
    <div class="dialog" :class="{ 'dialog-animate': animate }">
      <div class="dialog-header">
        <h3>{{ title }}</h3>
        <button @click="close" class="close-btn">×</button>
      </div>
      <div class="dialog-body">
        <slot></slot>
      </div>
      <div class="dialog-footer">
        <button @click="close">{{ cancelText }}</button>
        <button @click="confirm" :disabled="loading">
          {{ loading ? loadingText : confirmText }}
        </button>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: 'Dialog',
  props: {
    title: String,
    visible: Boolean,
    cancelText: {
      type: String,
      default: '取消'
    },
    confirmText: {
      type: String,
      default: '确认'
    },
    loading: {
      type: Boolean,
      default: false
    },
    loadingText: {
      type: String,
      default: '加载中...'
    },
    animate: {
      type: Boolean,
      default: true
    }
  },
  methods: {
    close() {
      this.$emit('update:visible', false);
    },
    confirm() {
      this.$emit('confirm');
    }
  }
};
</script>

<style scoped>
.dialog-animate {
  animation: fadeIn 0.3s;
}

@keyframes fadeIn {
  from {
    opacity: 0;
    transform: translateY(-20px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}
</style>

通过以上方法,可以手动实现一个功能丰富且灵活的 Vue Dialog 组件,满足不同场景的需求。

标签: vuedialog
分享给朋友:

相关文章

vue实现路由导航

vue实现路由导航

路由导航的实现方式 在Vue中,路由导航主要通过Vue Router实现。Vue Router是Vue.js官方的路由管理器,用于构建单页面应用(SPA)。 基本路由配置 安装Vue Router…

vue怎么实现文件树

vue怎么实现文件树

Vue 实现文件树的方法 使用递归组件 递归组件是处理嵌套数据结构的理想方式。文件树通常包含文件夹和文件的嵌套结构,适合用递归组件实现。 <template> <ul>…

vue路由实现内部切换

vue路由实现内部切换

Vue 路由实现内部切换的方法 Vue Router 是 Vue.js 官方的路由管理器,用于实现单页应用(SPA)的页面切换。以下是几种常见的内部路由切换方式: 使用 <router-lin…

vue实现冰墩墩

vue实现冰墩墩

使用 Vue 实现冰墩墩动画 通过 Vue 的动画特性和 SVG 技术,可以动态渲染冰墩墩的形象。以下是具体实现方法: 准备 SVG 素材 冰墩墩的形象通常由多个 SVG 路径组成,包括身体轮廓…

vue 实现在线预览

vue 实现在线预览

Vue 实现在线预览的常见方法 使用 iframe 嵌入 通过 iframe 标签可以嵌入多种类型的文件,如 PDF、图片、网页等。需要确保文件地址可访问。 <template>…

vue状态管理怎么实现

vue状态管理怎么实现

Vue 状态管理实现方法 使用 Vuex(官方推荐) Vuex 是 Vue 的官方状态管理库,适合中大型应用。 安装 Vuex: npm install vuex --save 创建 Store 示…