vue弹出框的封装

本文介绍了一个基于Vue的确认框组件实现方式,该组件具备自定义样式和过渡效果,可通过属性配置不同类型的提示信息及回调函数。

依旧是百度不到自己想要的,就自己动手丰衣足食

弹出框做成单独的组件confirm.vue;

<template>
    <transition name="mask-bg-fade">
        <div class="mask" v-show="show">
            <div class="mask_bg"></div>
            <transition name="slide-fade">
                <div class="modelBox" v-show="show">
                    <div class="tipIcon" v-bind:class="confirmModalOptions.type||'warning'"></div>
                    <div class="closeModel" v-on:click="closeModel()"></div>
                    <div class="title">{{confirmModalOptions.title || "提示"}}</div>
                    <div class="message textCenter">{{confirmModalOptions.message || " "}}</div>
                    <div class="model_btnBox">
                        <button class="dh_button_default2" v-on:click="confirmCancel()">
                            {{confirmModalOptions.btnCancelText || "取消"}}
                        </button>
                        <button class="dh_button_blue" v-on:click="confirmSubmit()">
                            {{confirmModalOptions.btnSubmitText || "确定"}}
                        </button>
                    </div>
                </div>
            </transition>
        </div>
    </transition>
</template>
<script>
  export default {
    props: ["confirmModalOptions"],
    data() {
      return {
        show: false,   // 是否显示模态框
      }
    },
    methods: {
      closeModel: function () {
        this.show = false;
      },
      showModel: function () {
        this.show = true;
      },
      confirmCancel: function () {
        this.show = false;
        if(typeof (this.confirmModalOptions.btnCancelFunction)==='function'){
          this.confirmModalOptions.btnCancelFunction()
        }
      },
      confirmSubmit: function () {
        this.show = false;
        if(typeof (this.confirmModalOptions.btnSubmitFunction)==='function'){
          this.confirmModalOptions.btnSubmitFunction()
        }
      }
    }
  }
</script>
<style lang="scss">

</style>

页面

<d-confirm v-bind:confirmModalOptions="confirmOptions" ref="myConfirm"></d-confirm>

import  DConfirm from 'components/confirm';
components: { DPromise,},

del: function () {
  this.$refs.myConfirm.showModel();
  this.confirmOptions= {
    type: "warning",//warning
    title: "提示",//提示
    message: "文字内容",//""
    btnCancelText: "取消",//取消
    btnSubmitText: "确定",//确定
    btnSubmitFunction: function () {
      alert("确定")
    },
    btnCancelFunction: function () {
      alert("取消")
    }
  }
},

添加了两个过度效果,背景和弹出框是分开的不同效果,自己可以再封装成一个甚至多个按钮的弹出框,自己就是遇到一些比较麻烦的问题,希望大神指导学习学习,

至于css样式就大家自己写吧;

欢迎批评指正

### 封装 Vue3 和 Element Plus 的弹出框组件 在 Vue 3 和 Element Plus 中封装一个弹出框组件可以极大地提高开发效率并保持代码的一致性和可维护性。以下是关于如何创建这样一个自定义弹出框组件的详细说明。 #### 组件结构设计 为了实现弹出框的功能,通常会基于 `el-dialog` 进行扩展和封装。这种封装可以通过传递属性、事件以及插槽来增强灵活性[^2]。 #### 实现步骤详解 1. **基础模板** 创建一个新的 Vue 文件作为弹出框的基础组件文件,命名为 `CustomDialog.vue`。 ```vue <template> <el-dialog :title="title" v-model="visible" width="50%" @close="handleClose"> <!-- 插槽用于动态传入内容 --> <slot></slot> <!-- 可选的操作按钮区域 --> <template #footer> <span class="dialog-footer"> <el-button @click="cancel">取 消</el-button> <el-button type="primary" @click="confirm">确 定</el-button> </span> </template> </el-dialog> </template> <script> export default { props: { title: { type: String, required: true }, // 对话框标题 modelValue: { type: Boolean, required: true } // 控制显示状态 }, emits: ['update:modelValue', 'confirmed'], // 自定义事件声明 computed: { visible: { get() { return this.modelValue; }, set(value) { this.$emit('update:modelValue', value); } } }, methods: { handleClose() { this.visible = false; // 关闭对话框逻辑 }, cancel() { this.handleClose(); // 用户点击取消时关闭窗口 }, confirm() { this.$emit('confirmed'); // 发射确认事件供父级处理业务逻辑 } } }; </script> ``` 上述代码展示了如何利用 `v-model` 来绑定可见性状态,并通过 `emits` 提供外部交互能力。 2. **使用示例** 下面是一个简单的例子展示如何在其他页面中引入该组件: ```vue <template> <div> <el-button @click="openDialog">打开弹窗</el-button> <custom-dialog title="这是一个测试弹窗" v-model="isDialogVisible" @confirmed="onConfirm"> <p>这里是弹窗的内容。</p> </custom-dialog> </div> </template> <script> import CustomDialog from './components/CustomDialog.vue'; export default { components: { CustomDialog }, data() { return { isDialogVisible: false, }; }, methods: { openDialog() { this.isDialogVisible = true; }, onConfirm() { console.log('用户已确认'); this.isDialogVisible = false; } } }; </script> ``` 此部分演示了如何将封装好的弹出框嵌套到实际应用当中。 #### 注意事项 - 确保所有的 prop 都有默认值或者必要的校验机制以防止运行错误。 - 如果需要更复杂的场景支持(比如异步加载数据),可以在内部加入额外的状态管理逻辑。 ---
评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值