当前位置: 首页 > news >正文

Vue2封装自定义全局Loading组件

前言

在开发的过程中,点击提交按钮,或者是一些其它场景总会遇到Loading加载框,PC的一些UI库也没有这样的加载框,无法满足业务需求,因此可以自己自定义一个,实现过程如下。

效果图

如何封装?

第1步:创建要封装成全局组件的文件

<template><div class="loading"v-show="msg.show"><div class="load-box"><!-- 图片放在文末,自行右键另存为 --><img src="@/assets/common/load.png"><span>{{ msg.title }}</span></div></div>
</template><script>
export default {name: 'SelfLoading',props: {msg: {type: Object,default: () => ({show: false,title: '加载中...'})}},methods: {// 显示loading的方法show (title = '加载中...') {this.msg.show = truethis.msg.title = title},// 隐藏loading的方法hide () {this.msg.show = false}}
}
</script><style lang="scss" scoped>
.loading {width: 100%;height: 100%;position: fixed;top: 0;left: 0;display: flex;justify-content: center;align-items: center;z-index: 9999;.load-box {background-color: rgba(0, 0, 0, 0.5);width: 100px;height: 100px;border-radius: 5px;box-shadow: 0px 1px 15px rgba(0, 0, 0, 0.5);color: #fff;display: flex;flex-direction: column;align-items: center;justify-content: center;letter-spacing: 0.8px;font-size: 13px;img {width: 30px;margin-bottom: 8px;animation: rotate 0.8s linear infinite;}}
}@keyframes rotate {to {transform: rotate(360deg);}
}
</style>

第2步:注册组件

Loading这类的通用组件一般定义为全局组件,那么直接在main.js文件中全局注册。

import SelfLoading from '@/components/Loading'
Vue.component('SelfLoading', SelfLoading)

第3步:使用组件

<template><!-- 全局Loading --><self-loading ref="loadingRef" />
</template><script>
export default {// 仅做示例created () {// 开启全局Loading,不传参默认为 '加载中...'this.$refs.loadingRef.show('上传中...')},// 仅做示例beforeDestroy () {// 隐藏全局Loadingthis.$refs.loadingRef.hide()}
}
</script>

图片在这里

图片右击另存为即可,好像会有CSDN自动添加的水印,不太知道怎么去除,需要原图的可以在评论区留下你的邮箱地址。

http://www.lryc.cn/news/99661.html

相关文章:

  • docker 搭建jenkins
  • 【Docker】Docker 部署 Mysql 并设置数据持久化
  • 【ARM 常见汇编指令学习 5 -- arm64汇编指令 wzr 和 xzr】
  • 4.4 成员变量与局部变量的区别有哪些?
  • 学生管理系统-03项目案例(3)
  • Banana Pi BPI-KVM – 基于 Rockchip RK3568 SoC 的 KVM over IP 解决方案
  • 面试:Spring Cloud和Kubernetes的优缺点
  • TSINGSEE青犀视频安防监控视频平台EasyCVR新增密码复杂度提示
  • 前端开发中的正则表达式:解密规则的魔法
  • const的用法
  • 机器学习深度学习——模型选择、欠拟合和过拟合
  • IP 服务级别协议监控
  • Emvirus: 基于 embedding 的神经网络来预测 human-virus PPIs【Biosafety and Health,2023】
  • 安全文件传输:如何降低数据丢失的风险
  • AI绘画StableDiffusion实操教程:可爱头像奶茶小女孩(附高清图片)
  • java8 GroupingBy 用法大全
  • vue_router__WEBPACK_IMPORTED_MODULE_1__.default is not a constructor
  • 前端html2canvas和dom-to-image实现截图功能
  • Hadoop平台集群之间Hive表和分区的导出和导入迁移(脚本)
  • Linux C语言实践eBPF
  • 垃圾回收标记阶段算法
  • 泰晓科技发布 Linux Lab v1.2 正式版
  • 王道数据结构-代码实操1(全注解版)
  • flink写入到kafka 大坑解析。
  • MATLAB算法实战应用案例精讲-【深度学习】预训练模型-Subword
  • 【HarmonyOS】实现从视频提取音频并保存到pcm文件功能(API6 Java)
  • Linux:shell命令运行原理和权限的概念
  • Javascript -- 数组prototype方法探究
  • android stduio 打开工程后直接报Connection refused解决
  • 搜索与图论(一)