uniapp的H5如何实现全局组件加载,类似uni.showToast?

在项目components文件夹新建一个base-loading文件夹,文件包括两个文件

第一个文件base-loading.vue

javascript 复制代码
<template>
	<u-overlay :show="visible" opacity="0.5">
		<view class="base-loading" v-show="visible">
			<base-image :src="loadingSrc" width="150px" height="150px" :loop="true"></base-image>
			<view class="text">
				{{ text }}
			</view>
		</view>
	</u-overlay>
</template>

<script>
	import {
		EventBus
	} from './loading.js';
	export default {
		name: "base-loading",
		data() {
			return {
				loadingSrc: `/static/loading.gif`, // 图片自定义替换
				visible: false, // 控制显示/隐藏
				text: ""
			};
		},
		created() {},
		methods: {}
	}
</script>

<style lang="scss">
	.base-loading {
		height: 100%;
		width: 100%;
		z-index: 999999;
		position: absolute;
		top: 0;
		display: flex;
		flex-direction: column;
		justify-content: center;
		align-items: center;
		
		.text {
			color: #e6e6e6; // #338CED
			font-size: 16px;
			letter-spacing: 1px; 
			font-weight: 600;
		}
	}
</style>

第二个文件 loading.js

javascript 复制代码
// import Vue from 'vue';

// // 创建一个 Vue 实例作为事件总线
// export const EventBus = new Vue();

// // 加载动画组件
// const LoadingComponent = Vue.extend(Loading);
// let loadingInstance;

// export function showLoading() {
// 	if (!loadingInstance) {
// 		loadingInstance = new LoadingComponent({
// 			el: document.createElement('div'),
// 		});
// 		document.body.appendChild(loadingInstance.$el);
// 	}
// 	instance.text = text;
// 	  instance.visible = true;
// }

// export function hideLoading() {
// 	if (loadingInstance) {
// 		EventBus.$emit('hide-loading');
// 	}
// }

import Vue from 'vue';
import LoadingComponent from './base-loading.vue'

// 创建一个Loading实例
const LoadingConstructor = Vue.extend(LoadingComponent);

let instance = null;

let timer = null

// 初始化Loading实例
function initInstance() {
	instance = new LoadingConstructor({
		el: document.createElement('div'),
	});
	document.body.appendChild(instance.$el);
}

// 显示Loading
function showLoading(text = '正在加载中...', time = 6000) {
	if (!instance) {
		initInstance();
	}
	instance.text = text;
	instance.visible = true;
	timer = setTimeout(() => {
		console.log("loading 自动关闭 --->", time);
		hideLoading()
	}, time)
}

// 隐藏Loading
function hideLoading() {
	if (instance) {
		instance.visible = false;
		clearTimeout(timer)
		timer = null
	}
}

export {
	showLoading,
	hideLoading
};

使用:

直接挂载在main.js页面

javascript 复制代码
// 载入加载弹窗
import mLoading from '@/components/base-loading/loading.js';
uni.y = mLoading

各个页面使用

javascript 复制代码
uni.y.showLoading('正在加载中...') //不传 默认正在,加载中...
uni.y.hideLoading()
相关推荐
哎哟喂呢哈6 分钟前
VedioJS 10
javascript·html5
仿生狮子5 小时前
OpenAI 攻下千禧年难题之后,开发者似乎无动于衷
前端·数学·vibecoding
夏幻灵8 小时前
JavaScript this 面试:五种绑定规则、优先级与常见面试陷阱
开发语言·javascript·面试
Lvan的前端笔记8 小时前
docker:每个前端项目一个 Nginx 容器还是只有一个Nginx容器
前端·nginx·docker
三天不学习9 小时前
Egg.js 4 突然爆火,原因是否归结于AI 原生落地需求爆发?
前端·javascript·全栈·egg.js
xcs194059 小时前
前端 vue 的前端页面debugger 进不去
前端·javascript·vue.js
明月_清风10 小时前
Deno 终局来了:从挑战 Node 到被 Cloudflare 收编
前端·后端·node.js
Csvn11 小时前
框架性能优化
前端
回眸&啤酒鸭13 小时前
【回眸】OpenSwarm 多智能体协作系统实战指南
大数据·前端·人工智能
用户693717500138413 小时前
2026,程序员的时代拐点到了
android·前端·后端