vue2和vue3的全局弹窗组件封装

为什么要封装组件

封装组件是为了能够更加便捷、快速的进行业务功能的开发。组件可以实现一些类似功能的复用及与其它业务逻辑的解耦。在开发中,我们难免会写很多类似的、重复的代码,有些相似的功能但涉及的字段有一些不一样。这时候可以把那些相同的功能,抽出来封装成组件,通过组件引用方式就很省事了。

confirm.vue组件内容

<template>
    <div class="complant" v-if="isShow">
        <div class="complant_main">
            <div class="popup_top">{{ text.title }}</div>
            <div class="complant_inner">
                <div class="popup_mes" v-if="text.mesg != ''">{{ text.mesg }}</div>
                <div class="popup_html" v-if="text.cntMsg != ''" v-html="text.cntMsg" ></div>
                <div class="popup_btn">
                    <input 
                        type="button" 
                        class="cancel"
                        :value="text.btn.cancelVal" 
                        @click="cancel"
                        :style="text.cancelValStyle"
                        v-if="text.cancelBtn">
                    <input 
                        type="button" 
                        :value="text.btn.confirmVal" 
                        @click="confirm"
                        :style="text.confirmValStyle"
                        :class="{ confirm: text.cancel == false }"
                        v-if="text.confirmBtn">
                </div>
            </div>
        </div>
    </div>
</template>

<script>
    export default {
        data() {
            return {
                isShow: true,
                text: {
                    title: "",
                    mesg: "",
                    cntMsg: "",
                    cancelBtn: true,
                    confirmBtn: true,
                    cancelValStyle: null,
                    confirmValStyle: null,
                    btn: {
                        confirmVal: "确定",
                        cancelVal: "取消"
                    }
                }
            }
        },
        methods: {
            cancel() {
                this.isShow = false;
            },
            confirm() {
                this.isShow = false;
            }
        },
    }
</script>

<style scope lang="less">
    .complant{
        width: 100%;
        height: 100%;
        position: absolute;
        top: 0;
        left: 0;
        background-color: #00000040;

        .complant_main{
            width: 300px;
            background: #fff;
            text-align: center;
            border-radius: 10px;
            position: absolute;
            top: 50%;
            left: 50%;
            transform: translate(-50%, -50%);
            overflow: hidden;

            .popup_top{
                line-height: 40px;
                background-color: #40a9ff;
                color: #fff;
            }

            .complant_inner{
                padding: 10px 0;
                div{
                    margin: 10px 0;
                }

                .popup_btn{
                    input{
                        background-color: #40a9ff;
                        color: #fff;
                        padding: 6px 10px;
                        border-radius: 6px;
                        border-color: initial;
                        margin: 0 10px;
                    }
                }
            }
        }
    }
</style>

confirm.js文件

vue2封装组件是基于Vue.extend的封装

import Vue from 'vue';
import confirm from '../../components/confirm';

let confirmConstructor = Vue.extend(confirm);

let theConfirm = function (text) {
    return new Promise((res, rej) => { //promise封装,ok执行resolve,no执行rejectlet
        let confirmDom = new confirmConstructor({
            el: document.createElement('div')
        })
        document.body.appendChild(confirmDom.$el);  //new一个对象,然后插入body里面
        confirmDom.text = Object.assign({},confirmDom.text, text);   //为了使confirm的扩展性更强,这个采用对象的方式传入,所有的字段都可以根据需求自定义
        confirmDom.confirm = function () {
            confirmDom.isShow = false;
            res("确认")
        }
        confirmDom.cancel = function () {
            confirmDom.isShow = false;
            rej("取消")
        }

    })
}

export default theConfirm;

由于vue3取消了extend,获取组件的$el需要换个写法

import { createApp } from "vue";
import confirmCon from '../../components/confirm';

let confirmDom;

// 导出函数可通过调用 Confirm() 函数动态创建 XtxConfirm 组件
const Confirms = (text) => {
    return new Promise((res, rej) => { //promise封装,ok执行resolve,no执行rejectlet
        confirmDom = createApp(confirmCon).mount(document.createElement("div"));
        confirmDom.text = Object.assign({},confirmDom.text, text); ;
        document.body.appendChild(confirmDom.$el);
        confirmDom.confirm = function () {
            confirmDom.isShow = false;
            res("确认")
        }
        confirmDom.cancel = function () {
            confirmDom.isShow = false;
            rej("取消")
        }
    })
}
export default Confirms

在man.js注册

import confirmjs from './assets/js/confirm.js'

//vue2写法
Vue.prototype.$confirm = confirmjs;

//vue3写法
const app = createApp(App);
app.config.globalProperties.$confirm = confirmjs;

最后在页面需要的时候,this可以直接用

<button @click="defaults">点我</button>

export default {
  methods: {
    defaults() {
      this.$confirm({
        title: "信息",
        mesg: "确定删除吗?",
        cntMsg: '<div>你好,插入一个元素</div>',
        cancelValStyle: { color: "yellow" },
        btn: {
          confirmVal: "确认删除",
          cancelVal: "我再想想"
        }
      })
      .then((res) => {
        console.log("yes:",res);
      }).catch((err) => {
        console.log("no:",err);
      });
    }
  },
};
  • 1
    点赞
  • 31
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Vue3封装全局弹窗组件的步骤如下: 1. 创建一个Vue实例,作为全局弹窗组件的容器。可以使用`createApp`方法创建Vue实例,并将其挂载到一个DOM元素上。 2. 在全局弹窗组件上定义必要的属性和方法。比如,可以定义一个`visible`属性控制弹窗的显示与隐藏,一个`title`属性用于显示弹窗的标题,一个`content`属性用于显示弹窗的内容等。 3. 在全局弹窗组件内部实现弹窗的样式和交互逻辑。可以使用Vue的模板语法和样式定义实现弹窗的外观和样式效果,并通过Vue的响应式特性,实现弹窗的交互逻辑,比如点击关闭按钮时隐藏弹窗。 4. 添加全局方法,在Vue实例的原型上添加一个方法,用于在任意组件中调用弹窗组件。可以使用`app.config.globalProperties`来添加全局方法,以便在任何地方都可以访问到该方法。 5. 在组件中使用全局弹窗组件。在需要显示弹窗组件中,通过调用全局方法来调用弹窗组件。可以通过传递参数的方式,动态设置弹窗的内容和样式。 6. 在全局弹窗组件的内部实现弹窗的生命周期钩子函数,比如`mounted`函数用于在弹窗组件被挂载到DOM后执行相应的逻辑。 通过以上步骤,就可以封装一个可在任何组件中使用的全局弹窗组件。在使用过程中,只需要调用全局方法,传入相应的参数,即可显示自定义的弹窗内容和样式,提供更好的用户体验。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值