vue2 封装确认弹框 干货版

公司项目 遇到需要自定义确认弹框的需求 下面是我的解决方案
组件代码

<template>
    <el-dialog
        :visible.sync="visible"
        @close="handleClose"
        custom-class="myconfirm"
        :show-close="false"
        width="503px">
        <div slot="title" style="height: 68px;position: relative">
            <img :src="headerImg">
            <div class="mycancel" @click="cancel">
                <i class="el-icon-close"></i>
            </div>
        </div>
        <div slot="default" class="default">
            <img :src="tipImg">
            <div style="margin-top: 20px">{{message}}</div>
        </div>
        <span slot="footer" style="display: flex;justify-content: space-around">
            <el-button v-if="options.showCancelButton" plain class="buttonSize" @click="cancel">{{options.cancelButtonText}}</el-button>
            <el-button type="primary" class="buttonSize" @click="confirm">{{options.confirmButtonText}}</el-button>
        </span>
    </el-dialog>
</template>

<script>
    import headerInfo from "./header_info.png"
    import headerSuccess from "./header_success.png"
    import headerError from "./header_error.png"
    import tipInfo from "./tip_info.png"
    import tipSuccess from "./tip_success.png"
    import tipError from "./tip_error.png"
    export default {
        data() {
            return {
                visible: false,
                message: '',
                resolvePromise: null,
                options:{
                    showCancelButton:true,
                    type:"info",//info 提示  success  成功   error 服务器繁忙
                    confirmButtonText:"确 定",
                    cancelButtonText:"取 消"
                }
            };
        },
        computed:{
            headerImg(){
                if(this.options.type == "info"){
                    return headerInfo
                }else if(this.options.type == "success"){
                    return headerSuccess
                }else if(this.options.type == "error"){
                    return headerError
                }
                },
            tipImg(){
                if(this.options.type == "info"){
                    return tipInfo
                }else if(this.options.type == "success"){
                    return tipSuccess
                }else if(this.options.type == "error"){
                    return tipError
                }
            }
        },
        methods: {
            open(message,options) {
                this.message = message;
                if(options){
                    options.showCancelButton && (this.options.showCancelButton = options.showCancelButton)
                    options.type && (this.options.type = options.type)
                    options.confirmButtonText && (this.options.confirmButtonText = options.confirmButtonText)
                    options.cancelButtonText && (this.options.cancelButtonText = options.cancelButtonText)
                }
                this.visible = true;
                return new Promise((resolve, reject) => {
                    this.resolvePromise = { resolve, reject };
                });
            },
            confirm() {
                this.resolvePromise.resolve(true);
                this.visible = false;
            },
            cancel() {
                this.resolvePromise.reject(false);
                this.visible = false;
            },
            handleClose() {
                this.resolvePromise.reject(false);
            }
        }
    };
</script>
<style scoped lang="scss">
    ::v-deep .myconfirm .el-dialog__header{
        padding: unset !important;
        padding-bottom: unset !important;
    }
    ::v-deep .myconfirm .el-dialog__body{
        padding: unset !important;
    }
    ::v-deep .myconfirm .el-dialog__footer{
        padding-top: 20px !important;
    }
    .default{
        display: flex;
        flex-direction: column;
        align-items: center;
        background: linear-gradient(to bottom,#D9E9FF,#FFFFFF);
    }
    .mycancel{
        height: 100%;
        width: 95px;
        position: absolute;
        right: 0;
        top: 0;
        background: #D9E9FF;
        display: flex;
        justify-content: center;
        align-items: center;
        cursor: pointer;
    }
    .mycancel i{
        font-size: 28px;
    }
    .buttonSize{
        width: 156px;height: 40px
    }

    ::v-deep .el-dialog.myconfirm:not(.is-fullscreen){
        margin-top: 30vh !important;
    }
</style>

挂载到全局

//自定义弹框
import MyConfirm from "@/components/MyConfirm/index.vue"
//自定义弹框
Vue.prototype.$myconfirm = function (message,options) {
    const ConfirmConstructor = Vue.extend(MyConfirm);
    const instance = new ConfirmConstructor();
    instance.$mount();
    document.body.appendChild(instance.$el);
    return instance.open(message,options);
};

把里面的内容替换为自己需要的自定义内容即可 也可以根据需要修改
如有疑问 可以通过微信公众号"酷爱篮球的前端小李"联系到我

首先,你需要在父组件中引入 Dialog 组件,并在父组件的 template 中添加一个触发显示的按钮。例如: ```vue <template> <div> <button @click="showDialog">显示</button> <Dialog v-model="dialogVisible"></Dialog> </div> </template> ``` 在上面的代码中,我们添加了一个按钮,当用户点击该按钮时,会触发 showDialog 方法。同时,我们引入了一个名为 Dialog 的组件,并将它的显示与隐藏与一个名为 dialogVisible 的变量绑定了起来。 接下来,在父组件的 script 中,我们需要定义 showDialog 方法以及 dialogVisible 变量。例如: ```vue <script> import Dialog from './Dialog.vue' export default { components: { Dialog }, data() { return { dialogVisible: false } }, methods: { showDialog() { this.dialogVisible = true } } } </script> ``` 在上面的代码中,我们首先引入了 Dialog 组件,然后在 data 中定义了 dialogVisible 变量,并将其默认值设置为 false。接着,我们定义了一个名为 showDialog 的方法,该方法将 dialogVisible 设置为 true,从而显示 Dialog 组件。 最后,我们需要在 Dialog 组件中添加一个关闭按钮,并将其与 dialogVisible 变量绑定起来。例如: ```vue <template> <div> <div class="dialog-mask" v-if="visible" @click="close"></div> <div class="dialog-wrapper" v-if="visible"> <div class="dialog-content"> <div class="dialog-header"> <h3 class="dialog-title">Dialog 标题</h3> <button class="dialog-close-btn" @click="close">×</button> </div> <div class="dialog-body"> <p>Dialog 内容</p> </div> </div> </div> </div> </template> <script> export default { props: { visible: { type: Boolean, required: true } }, methods: { close() { this.$emit('update:visible', false) } } } </script> ``` 在上面的代码中,我们添加了一个名为 close 的方法,该方法会将 dialogVisible 设置为 false,并通过 $emit 方法将更新后的值传递给父组件。同时,我们在组件中添加了一个关闭按钮,并在按钮的 click 事件中调用 close 方法。 这样,当用户点击父组件中的按钮时,就会触发 showDialog 方法,从而显示 Dialog 组件。当用户点击中的关闭按钮时,会触发 close 方法,从而关闭
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值