Vue3封装确认弹框,提示弹框组件

        今天给大家分享一个vue3+typescript封装的确认弹框和提示弹框组件,有喜欢的小伙伴直接复制相应的代码拿到项目中使用。

<template>
    <Transition class="dialog">
        <div v-if="show" class="dialog-main">
            <div class="dialog-wrap">
                <h3 class="dialog-main-title" v-show="titleText != ''">{{ titleText }}</h3>
                <div v-if="contentText" class="dialog-main-content">{{ contentText }}</div>
                <slot v-else name="content"></slot>
                <div class="dialog-peration">
                    <div v-if="showType==='confirm'" class="cancel-btn" @click.stop="handleCancel">
                        <p class="btn-text cancel-btn-right">{{ cancelText }}</p>
                    </div>
                    <div class="confirm-btn" @click.stop="handleConfirm">
                        <p class="btn-text">{{ confirmText }}</p>
                    </div>
                </div>
            </div>
        </div>
    </Transition>
</template>
<style lang="scss" scoped>
// 进入和离开动画
.dialog-enter-active{
    animation: opacity 0.3s;
}
.dialog-leave-active{
    animation: outOpacity 0.2s;
}
.dialog-enter-active .dialog-wrap{
    animation: scale 0.3s;
}
.dialog-main {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background-color: rgba(0,0,0,0.5);
    z-index: 998;
}
// 包裹层样式
.dialog-wrap{
    position: absolute;
    top:28%;
    left:0;
    right: 0;
    width: 320px;
    margin: 0 auto;
    background-color: #fff;
    border-radius:8px;
    z-index: 999;
    //设置或检索是否允许用户选中文本
    user-select:none;
}
// 顶部标题部分
.dialog-main-title{
    padding-top: 5px;
    text-align: center;
    font-size: 14px;
    font-weight: 500;
    color: #333;
}
// 中间内容部分
.dialog-main-content{
    padding: 0 15px;
    padding-top: 10px;
    margin-bottom: 32px;
    text-align: center;
    font-size: 14px;
    color: rgb(100,101,102);
    line-height: 1,5;
}
// 底部按钮样式
.dialog-peration{
    display: flex;
    border-radius: 8px;
    border: 1px solid #eee;
}
.dialog-peration .cancel-btn, .confirm-btn{
    flex: 1;
}
.dialog-peration .confirm-btn{
    color: rgb(38, 134, 255);
}
.dialog-peration .btn-text{
    text-align: center;
    font-size: 14px;
    margin: 8px 0;
    padding: 6px 0;
}
// 取消按钮样式
.cancel-btn-right {
    border-right: 1px solid #eee;
}
// 进入的动画
@keyframes opacity{
    0% {
        opacity: 0;
    }
    100% {
        opacity: 1;
    }
}
@keyframes scale {
    0% {
        transform: scale(0);
    }
    60% {
        transform: scale(1.1);
    }
    100% {
        transform: scale(1);
    }
}
// 离开的动画
@keyframes outOpacity {
  0% {
    opacity: 1;
  }
  100% {
    opacity: 0;
  }
}
</style>
<script lang="ts" setup>
const props = defineProps({
    show:{
        type: Boolean,
        default: false
    },
    titleText: {
        type: String,
        default: '标题'
    },
    contentText: {
        type: String,
        default: ''
    },
    cancelText: {
        type: String,
        default: '取消'
    },
    confirmText: {
        type: String,
        default: '确认'
    },
    // 等于confirm是确认按钮,不等于confirm是提示按钮
    showType: {
        type: String,
        default: 'confirm'
    }
})
const emits = defineEmits(["update:show", "confirm", "close"]);
const handleCancel = () => {
  emits("update:show", false);
  emits("close");
};

const handleConfirm = () => {
  emits("update:show", false);
  emits("confirm");
};
</script>

dialogAssembly

参数

说明

类型

默认值

备注

defineProps

默认展示的字段

object

content

slot

controls

弹框按钮

defineProps

参数

说明

类型

默认值

备注

show

控制弹框显示和隐藏

boolean

false(隐藏)true(显示)

titleText

标题

string

标题

contentText

内容

string

cancelText

取消

string

取消

confirmText

确认

string

确认

showType

类型

string

confirm(确认框)

controls

参数

说明

类型

默认值

备注

handleCancel

取消隐藏弹框

function(record)

handleConfirm

取消隐藏弹框,按需传入相应的接口,参数等

function(record)

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
你可以使用 Element UI 中的 MessageBox 组件来实现注册成功框。示例代码如下: ```html <template> <div> <!-- 注册表单 --> <el-form ref="registerForm" :model="registerForm" label-width="80px"> <el-form-item label="用户名" prop="username"> <el-input v-model="registerForm.username"></el-input> </el-form-item> <el-form-item label="密码" prop="password"> <el-input type="password" v-model="registerForm.password"></el-input> </el-form-item> <!-- 其他表单项 --> <el-form-item> <el-button type="primary" @click="submitForm">注册</el-button> </el-form-item> </el-form> </div> </template> <script> export default { data() { return { registerForm: { username: '', password: '', // 其他表单项 } } }, methods: { submitForm() { // 提交表单逻辑 // 注册成功框 this.$confirm('注册成功!', '提示', { confirmButtonText: '去登录', cancelButtonText: '继续注册', type: 'success' }).then(() => { // 点击“去登录”按钮的回调函数 // 跳转到登录页 }).catch(() => { // 点击“继续注册”按钮的回调函数,可以不写 }) } } } </script> ``` 在 `submitForm` 方法中,我们使用了 MessageBox 组件出注册成功的提示框。其中,`$confirm` 方法用于确认框,第一个参数为提示内容,第二个参数为提示框标题,第三个参数为选项配置对象。我们设置了确认按钮文本为“去登录”,取消按钮文本为“继续注册”,确认按钮类型为 success。确认按钮的回调函数中可以写跳转到登录页的逻辑。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值