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()
相关推荐
微祎_1 分钟前
写给新手的 triton-inference-server-ge-backend:昇腾Triton推理服务后端到底是啥?
前端·人工智能·cann
烂不烂问厨房4 分钟前
两张图片拼接在一起中间有条白线
前端
掘金安东尼7 分钟前
浏览器跨域窗口通信技术调研:window.open 与 postMessage
前端
Highcharts.js2 小时前
缺失数据可视化图表开发实战|Highcharts创建人员出生统计面积图表示例
开发语言·前端·javascript·信息可视化·highcharts·图表开发
LaughingZhu9 小时前
Product Hunt 每日热榜 | 2026-05-21
前端·人工智能·经验分享·chatgpt·html
怕浪猫9 小时前
Electron 开发实战(一):从零入门核心基础与环境搭建
前端·electron·ai编程
小鹏linux10 小时前
Ubuntu 22.04 部署开源免费具有精美现代web页面的Casdoor账号管理系统
linux·前端·ubuntu·开源·堡垒机
前端若水11 小时前
会话管理:创建、切换、删除对话历史
前端·人工智能·python·react.js
Bigger11 小时前
mini-cc:一个轻量级 AI 编程助手的诞生
前端·ai编程·claude
涵涵(互关)11 小时前
Naive-ui树型选择器只显示根节点
前端·ui·vue