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动画效果实现方法示例

    vue动画效果实现方法示例

    这篇文章主要介绍了vue动画效果实现方法,结合完整实例形式分析了vue.js+animate.css实现的动画切换效果相关操作技巧,需要的朋友可以参考下
    2019-03-03
  • 前端插件库之vue3使用vue-codemirror插件的步骤和实例

    前端插件库之vue3使用vue-codemirror插件的步骤和实例

    CodeMirror是一款基于JavaScript、面向语言的前端代码编辑器,下面这篇文章主要给大家介绍了关于前端插件库之vue3使用vue-codemirror插件的步骤和实例,文中通过实例代码介绍的非常详细,需要的朋友可以参考下
    2022-07-07
  • 解决elementui导航折叠卡顿的问题

    解决elementui导航折叠卡顿的问题

    这篇文章主要介绍了解决elementui导航折叠卡顿的问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2024-07-07
  • webpack开发vue-cli的项目实践

    webpack开发vue-cli的项目实践

    本文主要介绍了webpack开发vue-cli的项目实践,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2023-07-07
  • vue使用js-audio-recorder实现录音功能

    vue使用js-audio-recorder实现录音功能

    这篇文章主要为大家详细介绍了vue如何使用js-audio-recorder实现录音功能,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下
    2023-12-12
  • vue项目配置国际化$t('')的介绍和用法示例

    vue项目配置国际化$t('')的介绍和用法示例

    这篇文章主要给大家介绍了关于vue项目配置国际化 $t('')的介绍和用法的相关资料,多语言和国际化现在已经成为一个网站或应用的必要功能之一,Vue作为一款流行的前端框架,在这方面也有着灵活的解决方案,需要的朋友可以参考下
    2023-09-09
  • Vue + Vite项目通过/dist子路径访问首页空白问题的完整分析与解决方案

    Vue + Vite项目通过/dist子路径访问首页空白问题的完整分析与解决方案

    在前端工程化实践中,将Vue应用构建后部署到 Nginx是一件再常见不过的事情,然而,当项目需要以子路径形式部署时,却经常会遇到页面能访问,但首页内容为空白,本文将围绕通过域名/dist/访问 Vue+Vite项目首页空白这一典型问题,并给出一套完整、可复用的解决方案
    2026-01-01
  • 详解vue axios中文文档

    详解vue axios中文文档

    本篇文章主要介绍了详解axios中文文档,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-09-09
  • vue-cli3 从搭建到优化的详细步骤

    vue-cli3 从搭建到优化的详细步骤

    这篇文章主要介绍了vue-cli3 从搭建到优化的详细步骤,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2019-01-01
  • Vue标签属性动态传参并拼接字符串的操作方法

    Vue标签属性动态传参并拼接字符串的操作方法

    这篇文章主要介绍了Vue标签属性动态传参并拼接字符串的操作方法,我们需要根据传入值的类型,在placeholder属性赋值"请输入长度",“请输入宽度”,"请输入厚度"等提示字符,本文通过实例代码介绍的非常详细,需要的朋友参考下吧
    2023-11-11

最新评论