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>
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
首先,你需要在父组件引入 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、付费专栏及课程。

余额充值