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

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

第一个文件base-loading.vue

<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

// 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页面

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

各个页面使用

uni.y.showLoading('正在加载中...') //不传 默认正在,加载中...
uni.y.hideLoading()

相关推荐

  1. #Uniapp:uni-app全局组件方法easycom

    2024-01-12 07:14:04       60 阅读
  2. uniapp实现图片懒 封装组件

    2024-01-12 07:14:04       31 阅读
  3. uniapp如何分包

    2024-01-12 07:14:04       55 阅读
  4. uniapp使用vue3打包H5,android或ios白屏

    2024-01-12 07:14:04       24 阅读
  5. 【React】使用 antd 组件实现 iframe 效果

    2024-01-12 07:14:04       26 阅读
  6. uniapp】Vue3移动端滚动 分页组件封装

    2024-01-12 07:14:04       40 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-01-12 07:14:04       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-01-12 07:14:04       106 阅读
  3. 在Django里面运行非项目文件

    2024-01-12 07:14:04       87 阅读
  4. Python语言-面向对象

    2024-01-12 07:14:04       96 阅读

热门阅读

  1. 146. LRU 缓存

    2024-01-12 07:14:04       62 阅读
  2. Android Studio出现闪退

    2024-01-12 07:14:04       58 阅读
  3. HTML date类型数前后端互传,页面显示date类型

    2024-01-12 07:14:04       51 阅读
  4. Springboot 中设置统一的返回格式

    2024-01-12 07:14:04       46 阅读
  5. 并发编程(三)

    2024-01-12 07:14:04       48 阅读
  6. 128. 最长连续序列

    2024-01-12 07:14:04       59 阅读
  7. OD(4)之libunwind打印堆栈信息

    2024-01-12 07:14:04       49 阅读
  8. C语言中socket模块、线程

    2024-01-12 07:14:04       48 阅读