封装组件之---弹框封装

通用的弹框组件封装:

// 弹窗
<template>
    <div class="modal shdialog" v-show="show">
        <div class="modal-content rowflex" :style="{width: w, height: h}">
            <div class="title size28">{{title}}</div>
            <slot name="content"></slot>
            <!-- 底部按钮 -->
            <div class="footer row-center" v-if="showfooter">
                <div class="row-around btnarea">
                    <el-button
                        type="primary"
                        plain
                        v-if="showbtn1"
                        @click="clickbtn1"
                        size="mini"
                    >{{btn1}}</el-button>
                    <el-button
                        type="primary"
                        v-if="showbtn2"
                        @click="clickbtn2"
                        class="ml46"
                        size="mini"
                    >{{btn2}}</el-button>
                </div>
            </div>
            <slot name="foot" v-else></slot>
        </div>
    </div>
</template>

<script>
export default {
    data() {
        return {
            show: false
        };
    },
    props: {
        //标题
        title: {
            type: String,
            default: "加载中..."
        },
        // content的宽
        w: {
            type: String,
            default: "8rem"
        },
        // content的高
        h: {
            type: String,
            default: "6rem"
        },
        // 是否显示footer
        showfooter: {
            type: Boolean,
            default: true
        },
        // 确认按钮的文本
        btn1: {
            type: String,
            default: "取 消"
        },
        // 是否显示确认按钮
        showbtn1: {
            type: Boolean,
            default: true
        },
        // 取消按钮的文本
        btn2: {
            type: String,
            default: "确 定"
        },
        // 是否显示取消按钮
        showbtn2: {
            type: Boolean,
            default: true
        },
        // 是否显示
        visible: {
            type: Boolean,
            default: false
        }
    },
    watch: {
        visible(newVal) {
            this.show = newVal;
        }
    },
    methods: {
        // 点击按钮1
        clickbtn1() {
            this.show = false;
            this.$emit("onbtnone");
        },
        // 点击按钮2
        clickbtn2() {
            this.show = false;
            this.$emit("onbtntwo");
        }
    }
};
</script>

<style lang="scss" scoped>
.modal {
    position: fixed;
    top: 0;
    bottom: 0;
    left: 0;
    right: 0;
    background: rgba(2, 24, 32, 0.69);
    z-index: 999;
    .rowflex {
        display: flex;
        flex-direction: column;
        justify-content: space-between;
    }
    .modal-content {
        padding: 0 0.36rem;
        position: absolute;
        top: 50%;
        left: 50%;
        transform: translate(-50%, -50%);
        width: 8rem;
        height: 6rem;
        border-radius: 0.08rem;
        background: #ffffff;
        .title {
            text-align: left;
            line-height: 0.88rem;
            font-weight: bold;
            color: $primary;
            height: 0.88rem;
            border-bottom: 0.01rem solid rgba(229, 229, 231, 1);
        }
        .footer {
            height: 0.88rem;
            border-top: 0.01rem solid rgba(229, 229, 231, 1);
            .btnarea {
                width: 2.72rem;
            }
        }
    }
}
</style>


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在Vue中,可以使用插槽封装弹框组件。以下是一个示例: ``` <template> <div> <button @click="showModal = true">打开弹框</button> <transition name="modal"> <div v-if="showModal" class="modal-mask"> <div class="modal-wrapper"> <div class="modal-container"> <div class="modal-header"> <slot name="header">默认标题</slot> <button class="modal-close" @click="showModal = false">X</button> </div> <div class="modal-body"> <slot name="body">默认内容</slot> </div> <div class="modal-footer"> <slot name="footer"> <button @click="showModal = false">关闭</button> </slot> </div> </div> </div> </div> </transition> </div> </template> <script> export default { data() { return { showModal: false } } } </script> ``` 这个组件包含了一个按钮,当用户点击按钮时,弹出一个模态框。模态框的内容由插槽来提供。组件模板中定义了三个插槽,分别是header、body和footer。如果用户没有提供插槽内容,那么组件就会显示默认内容。如果用户提供了插槽内容,那么组件就会显示用户提供的内容。 这个组件使用了Vue的过渡效果来实现弹框的动画效果。当弹框出现时,会有一个淡入的效果;当弹框关闭时,会有一个淡出的效果。这个过渡效果的名称是“modal”,可以在组件的样式中定义。 这个组件的使用方法如下: ``` <template> <div> <my-modal> <template v-slot:header> <h2>自定义标题</h2> </template> <template v-slot:body> <p>自定义内容</p> </template> <template v-slot:footer> <button @click="showModal = false">确认</button> </template> </my-modal> </div> </template> <script> import MyModal from './MyModal.vue' export default { components: { MyModal }, data() { return { showModal: false } } } </script> ``` 在这个示例中,我们使用了自定义组件MyModal,并为它提供了三个插槽:header、body和footer。我们在这些插槽中提供了自定义的内容,以替换默认的内容。最后,我们将MyModal组件包裹在一个div中,并将这个div显示在页面上。当用户点击按钮时,弹框就会出现,并显示用户提供的内容。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值