vue3+vite全局loading

devtools/2025/2/12 3:39:53/

vue3+vite全局loading

j-loading.vue组件

<template><transition enter-active-class="animate__animated animate__fadeIn"leave-active-class="animate__animated animate__fadeOut"><div class="root-box" v-if="show"><div class="wrap"><img src="../../assets/img/loading.gif"/></div></div></transition>
</template><script setup>
import {ref} from "vue"
const show = ref(false)const showLoading = () => {show.value = true
}
const hideLoading = (callback) => {show.value = falsecallback && setTimeout(() => callback(), 500)
}defineExpose({show,showLoading,hideLoading
})
//指令使用
// v-jwq-loading="jLoading"//函数使用
// import {getCurrentInstance} from 'vue'
// // 获取当前实例
// const {proxy} = getCurrentInstance()
// 全局显示自定义loading
// const showJwqLoading = () => {
//   proxy.$showLoading()
//   setTimeout(() => {
//     proxy.$hideLoading()
//   }, 2000)
// }
</script><style scoped lang="scss">
.animate__animated.animate__fadeIn {--animate-duration: 0.5s;
}.animate__animated.animate__fadeOut {--animate-duration: 0.5s;
}.root-box {position: absolute;left: 0;top: 0;bottom: 0;right: 0;margin: 0;background-color: rgba(255, 255, 255, 0.9);z-index: 2000;display: flex;justify-content: center;align-items: center;.wrap {width: 300px;height: 300px;display: flex;justify-content: center;align-items: center;overflow: hidden;img {width: 100%;//transform: scale(2.5);}}
}
</style>

jLoadding.js

import {createVNode, render, cloneVNode} from "vue"
import jLoading from "./j-loading.vue"export default {install(app) {// 使用vue底层的createVNode方法将组件渲染为虚拟节点const VNode = createVNode(jLoading)// 使用render函数将组件挂载到body中render(VNode, document.body)// 定义全局方法设置组件的显示和隐藏app.config.globalProperties.$showLoading = VNode.component.exposed.showLoadingapp.config.globalProperties.$hideLoading = VNode.component.exposed.hideLoadingconst weakMap = new WeakMap()// 自定义Loading指令app.directive("jwq-loading", {mounted(el) {if (weakMap.get(el)) return//  记录当前绑定元素的positionweakMap.set(el, window.getComputedStyle(el).position)},updated(el, binding) {const oldPosition = weakMap.get(el);// 如果不是position: relative或者absolute,就设置为relative// 这里的目的是确保loading组件正确覆盖当前绑定的元素if (oldPosition !== 'absolute' && oldPosition !== 'relative') {el.style.position = 'relative'}// 克隆一份loading元素,// 作用是当页面上有多个zx-loading时,每个dom都维护一份属于自己的loading,不会冲突const newVNode = cloneVNode(VNode)// 挂载当前节点render(newVNode, el)// 判断绑定的值if (binding.value) {newVNode.component.exposed.showLoading()} else {newVNode.component.exposed.hideLoading(() => {// 还原布局方式el.style.position = oldPosition})}}})}
}

main.js

import jLoading from "./components/j-loading/jLoading.js"
// 引入自定义的全局Loading
app.use(jLoading)

全局设置请求

import jLoading from "@/components/j-loading/j-loading.vue"
import {createVNode, render} from "vue";
const VNode = createVNode(jLoading)
// 使用render函数将组件挂载到body中
render(VNode, document.body)
//开启加载
const showLoading=()=>{VNode.component.exposed.showLoading()
}
//关闭加载
const hideLoading=()=>{VNode.component.exposed.hideLoading()
}
//如get请求演示
/*** 封装get方法* @param url* @param data* @returns {Promise}*/export function get(url, params = {}) {return new Promise((resolve, reject) => {showLoading()//开启loadingaxios.get(url, {params: params}).then(response => {hideLoading()//关闭loadingresolve(response);}).catch(err => {reject(err)})})
}

http://www.ppmy.cn/devtools/158101.html

相关文章

网络通信的精髓:透彻理解 TCP/IP 的三次握手与四次挥手

网络通信的精髓:透彻理解 TCP/IP 的三次握手与四次挥手** 引言 在浩瀚的网络世界中,信息如流水般穿梭于全球各地,支撑着我们日常的在线互动、数据传输和云端服务。而这一切高效、可靠的网络通信,都离不开一个幕后英雄——TCP/IP 协议栈。它犹如网络的“骨架”和“神经系统…

docker启动报错code=exited, status=1/FAILURE——问题排查

问题 在某台centos7机器上&#xff0c;启动docker服务 sudo systemctl start docker报下列错误&#xff1a; ● docker.service - Docker Application Container EngineLoaded: loaded (/usr/lib/systemd/system/docker.service; enabled; vendor preset: disabled)Active: …

java后端开发day13--面向对象综合练习

&#xff08;以下内容全部来自上述课程&#xff09; 注意&#xff1a;先有javabean&#xff0c;才能创建对象。 1.文字版格斗游戏 格斗游戏&#xff0c;每个游戏角色的姓名&#xff0c;血量&#xff0c;都不相同&#xff0c;在选定人物的时候&#xff08;new对象的时候&#…

STM32G4系列微控制器深度解析

目录 概述 1 核心架构创新 1. 1 混合信号处理能力 1.2 存储系统优化 2 关键外设特性 2.1 模拟前端革新 2.2 数字通信接口 3 应用场景优势 3.1 电机控制 3.2 数字电源 3.3 USB PD方案 4 开发资源支持 4.1 软件生态 4.2 调试与烧录 5 选型对比指南 总结 概述 本…

R18 2Rx XR devices

根据3GPP的定义non-RedCap XR 可穿戴 UE 的默认值为4Rx(对于强制使用4Rx 的频段),但是对于有些XR UE(例如XR眼镜),由于体积小要戴在头上,使用时,仅由耳朵/后方和鼻梁支撑,导致外形尺寸受限,可用于 Rx 的体积有限,所以就有了2Rx XR UE。至于为什么有个前缀non-Redcap,应…

Python截图轻量化工具

一、兼容局限性 这是用Python做的截图工具&#xff0c;不过由于使用了ctypes调用了Windows的API, 同时访问了Windows中"C:/Windows/Cursors/"中的.cur光标样式文件, 这个工具只适用于Windows环境&#xff1b; 如果要提升其跨平台性的话&#xff0c;需要考虑替换cty…

80.在 Vue3 中使用 OpenLayers 和 gifler 加载 GIF 动画

引言 在现代 web 开发中&#xff0c;地图和动态内容的结合为用户体验提供了丰富的交互性和视觉效果。OpenLayers 是一个强大的 JavaScript 库&#xff0c;广泛用于渲染地图&#xff0c;而 GIF 动画是一种常见的动态内容形式。在本文中&#xff0c;我们将演示如何在 Vue3 项目中…

【RabbitMQ】RabbitMQ的下载安装及使用

安装RabbitMQ 下载网站&#xff1a;https://www.rabbitmq.com/docs/install-windows 点击后&#xff0c;会直接定位到依赖介绍位置&#xff0c;告诉你需要安装Erlang 下载Erlang Erlang也是一种编程语言&#xff0c;只是比较小众&#xff0c;但其拥有极为出色的性能 这个网站是…