当前位置:首页 > VUE

vue 实现 confirm

2026-02-10 12:36:44VUE

Vue 实现 Confirm 弹窗

在 Vue 中实现 Confirm 弹窗可以通过以下几种方式:

使用第三方库

许多成熟的 UI 库如 Element UI、Ant Design Vue 等提供了现成的 Confirm 组件。

Element UI 示例:

this.$confirm('确认删除吗?', '提示', {
  confirmButtonText: '确定',
  cancelButtonText: '取消',
  type: 'warning'
}).then(() => {
  // 确认操作
}).catch(() => {
  // 取消操作
});

自定义组件实现

创建可复用的 Confirm 组件:

vue 实现 confirm

  1. 创建 Confirm.vue 组件:
    
    <template>
    <div class="confirm-modal" v-if="visible">
     <div class="confirm-content">
       <h3>{{ title }}</h3>
       <p>{{ message }}</p>
       <div class="confirm-buttons">
         <button @click="onCancel">{{ cancelText }}</button>
         <button @click="onConfirm">{{ confirmText }}</button>
       </div>
     </div>
    </div>
    </template>
export default { props: { visible: Boolean, title: String, message: String, confirmText: { type: String, default: '确定' }, cancelText: { type: String, default: '取消' } }, methods: { onConfirm() { this.$emit('confirm'); this.$emit('update:visible', false); }, onCancel() { this.$emit('cancel'); this.$emit('update:visible', false); } } } ```
  1. 全局注册为插件:
    
    // confirm-plugin.js
    import Vue from 'vue';
    import Confirm from './Confirm.vue';

const ConfirmPlugin = { install(Vue) { Vue.component('Confirm', Confirm);

Vue.prototype.$confirm = function(options) {
  return new Promise(resolve => {
    const ComponentClass = Vue.extend(Confirm);
    const instance = new ComponentClass({
      propsData: options
    });

    instance.$on('confirm', () => {
      resolve(true);
      instance.$destroy();
    });

    instance.$on('cancel', () => {
      resolve(false);
      instance.$destroy();
    });

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

} };

vue 实现 confirm

Vue.use(ConfirmPlugin);


3. 使用方式:
```javascript
// 组件内使用
this.$confirm({
  title: '提示',
  message: '确认删除吗?'
}).then(confirmed => {
  if (confirmed) {
    // 执行删除操作
  }
});

使用 Promise 封装

简单场景可以直接用 Promise 封装浏览器原生 confirm:

function confirm(message) {
  return new Promise(resolve => {
    const result = window.confirm(message);
    resolve(result);
  });
}

// 使用
confirm('确认删除吗?').then(result => {
  if (result) {
    // 确认操作
  }
});

使用 Teleport(Vue 3)

Vue 3 可以使用 Teleport 实现弹窗:

<template>
  <button @click="showConfirm = true">删除</button>

  <Teleport to="body">
    <Confirm 
      v-if="showConfirm"
      @confirm="handleConfirm"
      @cancel="showConfirm = false"
    />
  </Teleport>
</template>

<script>
import { ref } from 'vue';
import Confirm from './Confirm.vue';

export default {
  components: { Confirm },
  setup() {
    const showConfirm = ref(false);

    const handleConfirm = () => {
      // 执行操作
      showConfirm.value = false;
    };

    return { showConfirm, handleConfirm };
  }
}
</script>

选择哪种实现方式取决于项目需求,简单项目可以使用原生 confirm 或第三方库,需要定制化则推荐自定义组件实现。

标签: vueconfirm
分享给朋友:

相关文章

vue如何实现到期提醒

vue如何实现到期提醒

实现 Vue 到期提醒功能 使用计算属性计算剩余时间 在 Vue 组件中定义一个计算属性,计算目标日期与当前日期的差值。通过 Date 对象获取时间戳并计算剩余天数或小时数。 computed:…

vue实现滚动截屏

vue实现滚动截屏

实现滚动截屏的基本思路 滚动截屏通常指对超出可视区域的长页面进行完整截图。在Vue中,可以通过结合DOM操作、Canvas绘制及第三方库实现。以下是具体方法: 使用html2canvas库实现…

vue实现上移下移插件

vue实现上移下移插件

实现上移下移功能的 Vue 插件 在 Vue 中实现列表项的上移和下移功能,可以通过自定义指令或组件封装的方式完成。以下是两种常见实现方式: 基于数组操作的通用方法 通过操作数组索引实现元素位置交…

vue实现div

vue实现div

Vue 实现 DIV 元素的方法 在 Vue 中实现 DIV 元素可以通过多种方式,以下是几种常见的实现方法: 使用模板语法 在 Vue 的模板中直接使用 HTML 的 <div> 标…

vue实现闪烁

vue实现闪烁

Vue实现元素闪烁效果 使用CSS动画实现 通过Vue绑定class结合CSS动画实现闪烁效果,代码简洁且性能较好。 <template> <div :class="{ 'b…

vue实现tag

vue实现tag

Vue 实现标签(Tag)功能 在 Vue 中实现标签(Tag)功能可以通过多种方式完成,以下是一些常见的方法和实现步骤: 使用动态组件和 v-for 通过 v-for 指令动态渲染标签列表,结合样…