H5项目dialog弹框封装

需求:H5项目在退出登录时需要弹出弹框提示是否确认退出登录,但该项目体积较小,没有引入组件库,所以决定自己封装一个dialog弹框。

在这里插入图片描述

子组件

<template>
  <div v-show="dialogVisible">
        <div class="van-overlay" style="z-index: 2008;"></div>
        <div role="dialog" class="van-dialog" style="z-index: 2009;">
            <div class="van-dialog__content van-dialog__content--isolated">
            <div class="van-dialog__message">{{title}}</div>
            </div>
            <div class="van-hairline-top van-dialog__footer">
                <button class="van-button van-button--default van-button--large van-dialog__cancel" @click="submit('0')">
                    <div class="van-button__content">
                    <span class="van-button__text">{{tipA}}</span>
                    </div>
                </button>
                <button class="van-button van-button--default van-button--large van-dialog__confirm van-hairline-left" @click="submit('1')">
                    <div class="van-button__content">
                    <span class="van-button__text">{{tipB}}</span>
                    </div>
                </button>
            </div>
        </div>
  </div>
  
</template>

<script>
export default {
    name: 'dialog',
    props: {
        dialogVisible: {
            type: Boolean,
            default: false,
        },
        title: {
            type: String,
            default: '',
        },
        tipA: {
            type: String,
            default: '取消',
        },
        tipB: {
            type: String,
            default: '确定',
        },
    },
    data() {
        return {}
    },
    created() {
    },
    methods: {
        submit(item) {
            this.$emit('dialogSubmit',item)            
        }
    },
}
</script>

<style>
.van-button__content:before {
    content: " ";
}
.van-button__content {
    display: -webkit-box;
    display: -webkit-flex;
    display: flex;
    -webkit-box-align: center;
    -webkit-align-items: center;
    align-items: center;
    -webkit-box-pack: center;
    -webkit-justify-content: center;
    justify-content: center;
    height: 100%;
}
* {
    box-sizing: border-box;
    -webkit-tap-highlight-color: transparent;
}
.van-hairline, .van-hairline-bottom, .van-hairline-left, .van-hairline-right, .van-hairline-surround, .van-hairline-top {
    position: relative;
}
.van-dialog__footer {
    display: -webkit-box;
    display: -webkit-flex;
    display: flex;
    overflow: hidden;
    -webkit-user-select: none;
    user-select: none;
}
.van-dialog__message {
    -webkit-box-flex: 1;
    -webkit-flex: 1;
    flex: 1;
    max-height: 60vh;
    padding: 26px 24px;
    overflow-y: auto;
    font-size: 16px;
    line-height: 20px;
    white-space: pre-wrap;
    text-align: center;
    word-wrap: break-word;
    -webkit-overflow-scrolling: touch;
}
.van-dialog__content--isolated {
    display: -webkit-box;
    display: -webkit-flex;
    display: flex;
    -webkit-box-align: center;
    -webkit-align-items: center;
    align-items: center;
    min-height: 104px;
}
.van-dialog {
    position: fixed;
    top: 45%;
    left: 50%;
    width: 320px;
    overflow: hidden;
    font-size: 16px;
    background-color: #fff;
    border-radius: 16px;
    -webkit-transform: translate3d(-50%,-50%,0);
    transform: translate3d(-50%,-50%,0);
    -webkit-backface-visibility: hidden;
    backface-visibility: hidden;
    -webkit-transition: .3s;
    transition: .3s;
    -webkit-transition-property: opacity,-webkit-transform;
    transition-property: opacity,-webkit-transform;
    transition-property: transform,opacity;
    transition-property: transform,opacity,-webkit-transform;
}
.van-overlay {
    position: fixed;
    top: 0;
    left: 0;
    z-index: 1;
    width: 100%;
    height: 100%;
    background-color: rgba(0,0,0,.7);
}
.van-button {
    position: relative;
    display: inline-block;
    box-sizing: border-box;
    height: 44px;
    margin: 0;
    padding: 0;
    font-size: 16px;
    line-height: 1.2;
    text-align: center;
    border-radius: 2px;
    cursor: pointer;
    -webkit-transition: opacity .2s;
    transition: opacity .2s;
    -webkit-appearance: none;
}
.van-button--default {
    color: #323233;
    background-color: #fff;
    border: 1px solid #ebedf0;
}
.van-button--large {
    width: 100%;
    height: 50px;
}
.van-dialog__cancel, .van-dialog__confirm {
    -webkit-box-flex: 1;
    -webkit-flex: 1;
    flex: 1;
    height: 48px;
    margin: 0;
    border-right: 0;
}
.van-dialog__confirm, .van-dialog__confirm:active {
    color: #ee0a24;
}
.van-button:before {
    position: absolute;
    top: 50%;
    left: 50%;
    width: 100%;
    height: 100%;
    background-color: #000;
    border: inherit;
    border-color: #000;
    border-radius: inherit;
    -webkit-transform: translate(-50%,-50%);
    transform: translate(-50%,-50%);
    opacity: 0;
    content: " ";
}
</style>

父组件引入

<template>
    <myDialog :title="title" :tipA="tipA" :tipB="tipB" @dialogSubmit = "dialogSubmit" :dialogVisible = "dialogVisible">			</myDialog>
</template>
<script>
    import myDialog from '../../common/util/myDialog.vue';

    export default {
        data(){
            return {
                tipB: '残忍离开',
                tipA: '手误了~',
                title: '确认登出账户吗?',
                dialogVisible: false             
            }
        },
        components:{           
            myDialog
        },
        methods:{//所有方法集中定义
            //登出确认
            dialogSubmit(val) {
                if(val === '1'){
                    this.dialogVisible = false;
                    doLogoutC({

                    }).then(resp=> {
                        window.location.href = "/rights/c/Mine"
                    }).catch(err => {                       
                        this.$toast.showToast(err);
                    })
                }else{
                    this.dialogVisible = false;
                }
            },
            logout() {
                this.dialogVisible = true;
            },
        },
        created(){//页面渲染前执行
            //TODO
        },
        mounted() {//页面渲染后执行
        }
    }
</script>

<style>
    
</style>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值