Vue.js弹出模态框组件开发

本文介绍了一个基于Vue的对话框组件实现方式,该组件通过Promise模式处理用户交互,并提供了高度可配置化的显示选项。文章详细展示了如何使用此组件进行确认对话框的操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

<template>
<div class="dialog">
    <div class="mask"></div>
    <div class="dialog-content">
        <h3 class="title">{{ modal.title }}</h3>
        <p class="text">{{ modal.text }}</p>
        <div class="btn-group">
            <div class="btn" @click="cancel">{{ modal.cancelButtonText }}</div>
            <div class="btn" @click="submit">{{ modal.confirmButtonText }}</div>
        </div>
    </div>
</div>
</template>

<script>
export default {
    name: 'dialog',
    props: {
        dialogOption: Object
    },
    data() {
        return {
            resolve: '',
            reject: '',
            promise: '', // 保存promise对象
        }
    },
    computed: {
        modal: function() {
            let options = this.dialogOption;
            return {
                title: options.title || '提示',
                text: options.text,
                cancelButtonText: options.cancelButtonText ? options.cancelButtonText : '取消',
                confirmButtonText: options.confirmButtonText ? options.confirmButtonText : '确定',
            }
        }
    },
    methods: {
        //确定,将promise断定为完成态
        submit() {
            this.resolve('submit');
        },
        // 取消,将promise断定为reject状态
        cancel() {
            this.reject('cancel');
        },
        //显示confirm弹出,并创建promise对象,给父组件调用
        confirm() {
            this.promise = new Promise((resolve, reject) => {
                this.resolve = resolve;
                this.reject = reject;
            });
            return this.promise; //返回promise对象,给父级组件调用
        }
    }
}
</script>

<style lang="scss">
.dialog {
    position: relative;
.dialog-content {
position: fixed;
box-sizing: border-box;
padding: 20px;
width: 80%;
min-height: 140px;
left: 50%;
top: 50%;
transform: translate(-50%, -50%);
border-radius: 5px;
background: #fff;
z-index: 50002;
.title {
font-size: 16px;
font-weight: 600;
line-height: 30px;
}
.text {
font-size: 14px;
line-height: 30px;
color: #555;
}
.btn-group {
display: flex;
position: absolute;
right: 0;
bottom: 10px;
.btn {
padding: 10px 20px;
font-size: 14px;
&:last-child {
color: #76D49B;
}
}
}
}
.mask {
position: fixed;
top: 0;
left: 0;
bottom: 0;
right: 0;
z-index: 50001;
background: rgba(0,0,0,.5);
}
}
</style>




调用

<v-dialog v-show="showDialog" :dialog-option="dialogOption" ref="dialog"></v-dialog>
this.showDialog = true;
this.$refs.dialog.confirm().then(() => {
this.showDialog = false;
next();
}).catch(() => {
this.showDialog = false;
next();
})

评论 14
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值