vue结合el-dialog封装自己的confirm二次确认弹窗方式

 更新时间:2023年08月21日 14:37:58   作者:唐十八_wei  
这篇文章主要介绍了vue结合el-dialog封装自己的confirm二次确认弹窗方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教

vue结合el-dialog封装自己的confirm二次确认弹窗

这里使用el-dialog 主要是用他的关闭动画,让关闭更加丝滑

首先在components 添加 ConfirmAlert文件夹 然后添加vue和js 文件

在这里插入图片描述

index.js

import Vue from 'vue';
import confirm from './index.vue'
let confirmConstructor = Vue.extend(confirm);
let theConfirm = function (content) {
    return new Promise((res, rej) => {
        //返回promise,ok执行resolve,调用时使用.then继续,no执行reject调用时使用catch捕捉
        let confirmDom = new confirmConstructor({
            el: document.createElement('div')
        })
        const elDiv = document.body.appendChild(confirmDom.$el); //new一个对象,然后插入body里面
        confirmDom.content = content; //为了使confirm的扩展性更强,这个采用对象的方式传入,所有的字段都可以根据需求自定义
        confirmDom.ok = function () {
            res()
            close()
        }
        confirmDom.close = function () {
            rej()
            close()
        }
        function close() {
            confirmDom.dialogVisible = false
            setTimeout(() => {
                console.log('remove');
                elDiv.remove()
            }, 1000);
        }
    })
}
export default theConfirm;

index.vue

<template>
    <div class="confirm-container">
        <el-dialog :title="content.type" :before-close="close" :visible.sync="dialogVisible" width="30%">
            <div class="confirm-content">
                <p class="msgContent">{{ content.msg }}
                </p>
                <div class="foot" slot="footer">
                    <el-button type="primary" @click.stop="close">
                        <span>{{ content.btn.no || '确认' }}</span>
                    </el-button>
                    <el-button type="primary" @click.stop="ok">
                        <span>{{ content.btn.ok || '取消' }}</span>
                    </el-button>
                </div>
            </div>
        </el-dialog>
    </div>
</template>
<script>
export default {
    data() {
        return {
            content: {
                type: '提示',
                msg: '',
                btn: {
                    ok: '确定',
                    no: '取消'
                },
            },
            dialogVisible: true
        }
    },
    methods: {
        close() {
            console.log('关闭');
        },
        ok() {
            console.log('确定')
        }
    }
}
</script>
<style scoped>
.msgContent {
    text-align: center;
}
.foot {
    width: 100%;
    display: flex;
    justify-content: center;
    margin-top: 32px;
}
</style>

main.js将alertConfirm挂载vue上

import alertConfirm from '@/components/confirmAlert'
Vue.prototype.$alertConfirm = alertConfirm;

组件中使用

<!--  -->
<template>
    <div>
        <el-button @click="btn">log</el-button>
    </div>
</template>
<script>
export default {
    data() {
        return {
        };
    },
    watch: {},
    components: {},
    computed: {},
    created() { },
    mounted() { },
    methods: {
        btn() {
            let that = this // 存储this指向
            this.$alertConfirm({
                type: '提示',
                msg: '是否删除这条信息?',
                btn: {
                    ok: '确定', no: '取消'
                },
                //msgDetail: ''
            }).then(() => {
                // 确认
                // do something
                that.logs('确认')
            }).catch(() => {
                //取消
                // do something
                that.logs('取消')
            })
        },
        logs(type) {
            console.log('调用组件的' + type);
        }
    }
}
</script>
<!-- <style scoped>
</style> -->

vue二次确认弹窗组件

1.二次确认弹窗组件reconfirm.vue

<template>
  <el-dialog :visible="dialogFlag"  @close="closeDialog()" width="420px" class="myz-info-reconfirm">
    <div slot="title" class="title-reconfirm">
      <span>{{title}}</span>
      <span class="warn-red" v-if="deleteAdmin">({{redWarnMessage}})</span>
    </div>
    <div class="body_message">
      <span class="warn-img"></span>
      {{warnMessage}}
    </div>
    <span slot="footer" class="clearfix">
      <span class="check-exact"  v-if="deleteAdmin">
        <el-checkbox v-model="checked">我已了解</el-checkbox>
      </span>
      <span class="check-btn">
        <el-button type="primary" :disabled="!checked && deleteAdmin" @click="exact()">确定</el-button>
        <el-button  @click="closeDialog">取消</el-button>
      </span>
    </span>
  </el-dialog>
</template>
<script>
import { mapState, mapActions } from 'vuex'
import eventBus from '@/components/busTag' // 引入公共的bus,来做为中间传达的工具
export default {
  props: {
    title: {
      type: String,
      default: '提示'
    },
    warnMessage: {
      type: String
    },
    redWarnMessage: {
      type: String,
      default: ''
    },
    deleteInfo: { // 删除信息
      type: Object,
      default: () => {}
    }
  },
  components: {
  },
  data () {
    return {
      deleteAdmin: false,
      dialogFlag: false,
      checked: false
    }
  },
  computed: {
    ...mapState(['reConfirm'])
  },
  watch: {
    reConfirm (newVal) {
      this.dialogFlag = newVal
    }
  },
  created () {
  },
  mounted () {
    //删除后初始化条件
    eventBus.$on('deleteHandleSuccess', () => {
      this.checked = false
      this.deleteAdmin = false
    })
  },
  methods: {
    ...mapActions(['updateReconfirm']),
    closeDialog: function () {
      this.checked = false
      this.deleteAdmin = false
      this.updateReconfirm(false)
    },
    exact () {
      if (this.deleteInfo.adminUser && this.checked === false) {
        this.deleteAdmin = true
      } else {
        this.$emit('deleteHandle')
      }
    }
  }
}
</script>
<style lang="scss" scoped>
  .title-reconfirm{
    font-size: 18px;
  }
  .check-exact{
    float: left;
  }
  .check-btn{
    float: right;
  }
  .warn-red{
    font-size: 12px;
    color:red;
  }
  .body_message{
    padding-bottom: 30px;
    padding-left: 10px;
  }
  .warn-img{
    padding-right: 30px;
    display: inline-block;
    width: 25px;
    height:25px;
    background: url("/static/icon/common-icon/warning.png") no-repeat;
    border: none;
    background-size: 25px 25px;
    vertical-align: middle;
  }
</style>
<style lang="scss">
  .el-checkbox__inner{
    border: 1px solid #909399;
  }
  .myz-info-reconfirm div.el-dialog__footer{
    border:none;
    text-align: right;
    height: 50px;
    padding: 6px 0;
  }
  .myz-info-reconfirm div.el-dialog__header{
    border:none;
    padding: 6px 0;
  }
  .myz-info-reconfirm div.el-dialog__body{
    padding: 20px;
  }
  .myz-info-reconfirm div.el-dialog{
    margin-top: 35vh !important;
  }
/*清除浮动-start*/
.clearfix {
  *zoom: 1;
}
.clearfix:before,.clearfix:after {
  display: block;
  overflow: hidden;
  clear: both;
  height: 0;
  visibility: hidden;
  content: ".";
}
/*清除浮动-end*/
</style>

2.引用二次弹窗组件

<template>
  <div>
    <reconfirm @deleteHandle="deleteHandle" :warnMessage="'确定删除提示'" :deleteInfo="deleteLabelInfo" :redWarnMessage="'二次提示信息'"></reconfirm>
  </div>
</template>
<script>
import { mapActions } from 'vuex'
import Reconfirm from '@/components/Reconfirm'
export default {
  components: {
    Reconfirm
  },
  data () {
    return {
      deleteLabelInfo: {}
    }
  },
  watch: {
  },
  mounted () {
  },
  computed: {
  },
  methods: {
    ...mapActions(['updateReconfirm']),
    deleteHandle () {
       //处理删除逻辑
    },
    deleteAppLabel () {
      //显示二次提示条件adminUser
      this.deleteLabelInfo.adminUser = (this.TeamLabelTeams.length > 0)
      //显示二次提示弹窗
      this.updateReconfirm(true)
    }
  }
}
</script>
<style lang="scss" scoped>
</style>

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。

相关文章

  • 使用 Vue cli 3.0 构建自定义组件库的方法

    使用 Vue cli 3.0 构建自定义组件库的方法

    本文旨在给大家提供一种构建一个完整 UI 库脚手架的思路。通过实例代码给大家讲解了使用 Vue cli 3.0 构建自定义组件库的方法,感兴趣的朋友跟随小编一起看看吧
    2019-04-04
  • element组件el-date-picker禁用当前时分秒之前的日期时间选择

    element组件el-date-picker禁用当前时分秒之前的日期时间选择

    本文主要介绍了element组件el-date-picker禁用当前时分秒之前的日期时间选择,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2023-01-01
  • vue3.0实现考勤日历组件使用详解

    vue3.0实现考勤日历组件使用详解

    这篇文章主要为大家详细介绍了vue3.0实现考勤日历组件使用,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2022-08-08
  • 带你一步步从零搭建一个Vue项目

    带你一步步从零搭建一个Vue项目

    Vue.js是现在比较优秀的Web前端框架,非常推荐大家入门学习,这篇文章主要给大家介绍了关于如何一步步从零搭建一个Vue项目的相关资料,文中通过图文以及实例代码介绍的非常详细,需要的朋友可以参考下
    2022-05-05
  • 基于vue 添加axios组件,解决post传参数为null的问题

    基于vue 添加axios组件,解决post传参数为null的问题

    下面小编就为大家分享一篇基于vue 添加axios组件,解决post传参数为null的问题,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2018-03-03
  • vue实现表格动态嵌入折线图的绘制代码

    vue实现表格动态嵌入折线图的绘制代码

    这篇文章给大家介绍了vue实现表格动态嵌入折线图的绘制方法,文中有详细完整的代码示例攻大家参考,对大家的学习或工作有一定的参考价值,需要的朋友可以参考下
    2023-10-10
  • vue中使用[provide/inject]实现页面reload的方法

    vue中使用[provide/inject]实现页面reload的方法

    这篇文章主要介绍了在vue中使用[provide/inject]实现页面reload的方法,文中给大家提到了在vue中实现页面刷新不同的方法,非常不错,具有一定的参考借鉴价值,需要的朋友可以参考下
    2019-09-09
  • 解决Vue2.0自带浏览器里无法打开的原因(兼容处理)

    解决Vue2.0自带浏览器里无法打开的原因(兼容处理)

    本篇文章主要介绍了解决Vue2.0自带浏览器里无法打开的原因(兼容处理),非常具有实用价值,需要的朋友可以参考下
    2017-07-07
  • vue封装一个弹幕组件详解

    vue封装一个弹幕组件详解

    这篇文章主要介绍了vue封装一个弹幕组件详解,文章围绕主题展开详细的内容介绍,具有一定的参考价值,需要的小伙可以参考一下
    2022-08-08
  • Vue 不定高展开动效原理详解

    Vue 不定高展开动效原理详解

    本文主要介绍了Vue不定高展开动效原理详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2022-06-06

最新评论