vue2和vue3的全局弹窗组件封装_vue3全局弹窗组件_自信酷的博客-程序员秘密

技术标签: 前端  vue.js  javascript  

为什么要封装组件

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

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);
      });
    }
  },
};
版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/qq_33235582/article/details/121144874

智能推荐

陷波器及其算法(基于C语言)_陷波器 c语言_Chillinglu的博客-程序员秘密

首先,陷波器的传递函数是:利用matlab对其进行离散化,tustin变换。syms w0 s Ts z xi % 定义符号变量G1 =(s^2+w0^2)/(s^2+2*w0*xi*s+w0^2) %传递函数sys_s2c = 2*(z-1)/Ts/(z+1)G2 = subs(G1,s,sys_s2c) %离散化 tustin变换G3 = colle...

使用Quick-Cocos2d-x搭建一个横版过关游戏(一) CCStore_阳光下的的博客-程序员秘密

我们打开config.lua文件,将里面的内容改成下面的:123456789101112131415161718192021222324-- 0 - disab

Linux平台如何编译使用Google test写的单元测试?_阿波321的博客-程序员秘密

本博客http://blog.csdn.net/livelylittlefish 贴出作者(三二一@小鱼)相关研究、学习内容所做的笔记,欢迎广大朋友指正! 在Linux平台如何编译单元测试的代码?(请参考readme文件)   Step1. 编译gtest-all.cc和gtest_main.cc文件 g++ -I${GTEST_DIR}/include -I${GTEST_DIR} -c ${GTEST_DIR}/src/gtest-all.cc g++ -I${GTEST_DIR}/includ

用zrender制作一个基础的绘图板,绘图板可用于组态界面的基础性开发_zrender group_扶我起来,我还要写一行代码的博客-程序员秘密

用zrender制作一个基础的绘图板,绘图板可用于组态界面的基础性开发,zrender开发文档地址:https://ecomfe.github.io/zrender-doc/public/api.html&lt;template&gt; &lt;div class="hello"&gt; &lt;div id="main"&gt; &lt;/div&gt;...

建行u盾弹不出来_关于建设银行网银的问题```_weixin_39634900的博客-程序员秘密

一共分几步走,就可以把问题解决了:一、准备工作 :1、点我的电脑→工具→文件夹选项→查看,去掉“隐藏受保护的操作系统文件”和“隐藏已知文件类型的扩展名”前的对勾。选中“显示所有文件和文件夹”。2、打开本地安全设置窗口,点开始→运行→输入 c /s→确定(或者点开始→控制面板→管理工具→本地安全策略)。3、打开本地安全设置窗口后,依次展开:计算机配置—WINDOWS设置→安全设置4、右键软件限制策略...

Maven之pom.xml继承父pom.xml配置,如何移除父pom中的依赖_maven排除父类pom中的引用的jar_Asker.J的博客-程序员秘密

文章目录前言一、项目间的依赖关系二、使用步骤1.父pom2.子POM总结子模块如何把父pom依赖的jar包排除掉maven 依赖管理原则——最短路径原则前言maven是java项目的管理和构建工具。我们可以通过引入不同依赖,以满足项目开发。项目越大,引入的依赖就越多,带来的管理问题也就越来越突出。因此,统一的依赖管理就显得尤为重要!提示:以下是本篇文章正文内容,下面案例可供参考一、项目间的依赖关系示例:二、使用步骤1.父pom使用dependencyManagement 和 plug

随便推点

嵌入式开发入门之经典 ARM开发板_iteye_1193的博客-程序员秘密

嵌入式开发入门之经典 开始进入嵌入式世界,真是一头雾水,不知道如何入手!也不知道该如何学习,学习什么,最近从网上转载这篇文章,对我启发很大,对于初始进入嵌入式的人们很有帮组,好多嵌入式大侠都说这是入门的最好文章. 1、抓住51开发ARM   这几个月来我一直都爬在51的问题,自己都有一点笑自己了,用了4个月的时间,来巩固51的原理和程序,还好我自己算是走过来了,自己笨,身边的高才生又看...

PyOpenGL三体模拟_三体问题模拟_〃kawehburg的博客-程序员秘密

给定多星系统的初始状态,以一定的时间步,计算在引力作用下的星体运动,并使用openGL实时可视化。实验环境python37+OpenGL https://www.cnblogs.com/GraceSkyer/p/9235582.htmlnumpyPIL初始条件使用一个数组p表示多星系统的初始条件p = [[1.0, 0, 0, 0, 0, 0, 0], [1.0, 0, 10, 0, 0.2, 0, 0], [1.0, 10, 0, 0, 0, 0, 0.2],

通过Fiddler(windows)抓http请求(android、浏览器)_fiddler windows_西京刀客的博客-程序员秘密

文章目录通过Fiddler(windows)抓的http请求配置什么是fidder常用场景官网下下载安装fidder配置和使用APP抓包时的手机代理设置通过Fiddler(windows)抓的http请求配置什么是fidderfiddler(fiddler中文版)是一个http协议调试代理工具。fiddler能够记录并检查所有你的电脑和互联网之间的http通讯。Fiddler是目前常用的抓包工具之一,作为客户端与服务器端http协议调试代理工具,记录着客户端与服务器端所有的请求,可以通过Fiddle

afxwin1.inl(1014) : error C4430: 缺少类型说明符 - 假定为 int。注意: C++ 不支持默认 int_vc afxwin1.inl_汪宁宇的博客-程序员秘密

错误信息:C:/Program Files/Microsoft Visual Studio/VC98/MFC/INCLUDE/afxwin1.inl(1014) : error C4430: 缺少类型说明符 - 假定为 int。注意: C++ 不支持默认 intC:/Program Files/Microsoft Visual Studio/VC98/MFC/INCLUDE/a

Qt:使用数据库(以MySQL为例),并需要将程序封装到其他设备上时,必要的一些依赖项_qt库依赖什么_SAW1113的博客-程序员秘密

在使用数据库(MySQL)后,想将程序完整的封装成一个可安装的应用,并移植到其他硬件设备上,必要的一些依赖项,及这些依赖项应该放置的位置进行总结。libmysql.dll这个不必多说,想要在Qt中使用数据库第一个接触的依赖项就是libmysql.dll来源:MySQL的安装目录下就可以找到。 封装时放置的位置:与XXXXXX.exe文件同目录即可。platforms文件夹下属dl...

推荐文章

热门文章

相关标签