uni-app+vue3+pina实现全局加载中效果,自定义全局变量和函数可供所有页面使用

server/2024/9/25 7:18:50/

首先自定义一个加载中组件

ccloading.vue

<template><view class="request-loading-view" v-if="loadingShow"><view class="loading-view"><image class="loading-img" :src="loading" mode="aspectFit"></image></view></view>
</template><script setup>
import loading from "@/assets/images/loading.gif"
import {useCommonStore} from "@/store/common";
import {computed} from "vue";
const commonStore = useCommonStore();
const loadingShow=computed(()=> commonStore.showLoading)
</script><style scoped>
.request-loading-view {position: fixed;top: 0;left: 0;width: 100vw;height: 100vh;z-index: 999999;background-color: #fff;display: flex;justify-content: center;align-items: center;
}.loading-view {width: 100rpx;height: 140rpx;/* background-color: rgba(0, 0, 0, 0.6); */border-radius: 20rpx;display: flex;justify-content: center;align-items: center;
}.loading-img {width: 100%;height: 100%;
}
</style>

然后再pina里面保存这个 showLoading的值。判断是否显示加载中。也就是上面引入的

/store/common.ts文件
import { defineStore } from 'pinia'
import piniaPersistConfig from "@/store/helper/persist";//因为persist不支持小程序的缓存。所以再这个页面重写了一下存储方式。参考这个export interface State {showLoading: boolean;
}
// @ts-ignore
export const useCommonStore = defineStore('pack-store', {persist: piniaPersistConfig("pack-store"),state: (): State => ({showLoading:false,//loading是否显示}),getters: {},actions: {setChangeLoading(data:boolean){console.log("changeLoading",data)this.showLoading = data;},},
});

 main.js定义全局组件。定义全局函数

import { createSSRApp } from "vue";
import  Vue from "vue";
import App from "./App.vue";
import store from "./store";import {useCommonStore} from "@/store/common";
import newRequestLoading from '@/components/ccloading/ccloading.vue';export function createApp() {const app = createSSRApp(App);app.use(store);app.component('new-loading', newRequestLoading);const commonStore = useCommonStore();// 添加全局属性方法app.config.globalProperties.$loadingStatus=commonStore.showLoading;app.config.globalProperties.$loading = {show() {commonStore.setChangeLoading(true);},hide() {commonStore.setChangeLoading(false);}}return {app,};
}

然后就可以在页面使用了

index.vue

<template><new-loading/>
</template>
<script setup lang="ts">
import {ref, reactive, getCurrentInstance} from "vue";
const instance = getCurrentInstance()?.proxy
onLoad((options) => {instance.$loading.show();//显示加载中getHide();
})

 const getHide=()=>{

instance.$loading.hide();//隐藏加载中

}


http://www.ppmy.cn/server/121705.html

相关文章

Ansible部署与应用基础

由于互联网的快速发展导致产品更新换代速度逐步增长&#xff0c;运维人员每天都要进行大量的维护操作&#xff0c;按照传统方式进行维护使得工作效率低下。这时部署自动化运维就 可以尽可能安全、高效的完成这些工作。 一、Ansible概述 1.什么是Ansible Ansible 是基于 Pytho…

网页护眼宝——全方位解析 Chrome Dark Reader 插件

网页护眼宝——全方位解析 Chrome Dark Reader 插件 1. 基本介绍&#xff1a;Chrome 插件的力量与 Dark Reader 的独特之处 随着现代浏览器的功能越来越强大&#xff0c;Chrome 插件为用户提供了极大的定制化能力。从广告屏蔽、性能优化到页面翻译&#xff0c;Chrome 插件几乎…

【图论】最短路应用

1135. 新年好 题目 提交记录 讨论 题解 视频讲解 MarkDown视图Copy 重庆城里有 nn 个车站&#xff0c;mm 条 双向 公路连接其中的某些车站。 每两个车站最多用一条公路连接&#xff0c;从任何一个车站出发都可以经过一条或者多条公路到达其他车站&#xff0c;但不同的…

JavaWeb--纯小白笔记06:使用Idea创建Web项目,Servlet生命周期,注解,中文乱码解决

使用Idea创建一个web项目----详细步骤配置&#xff0c;传送门&#xff1a;http://t.csdnimg.cn/RsOs7 src&#xff1a;放class文件 web&#xff1a;放html文件 out&#xff1a;运行过后产生的文件 一创建一个新的web项目(配置好了后)&#xff1a; 在src创建一个文件…

MySQL数据库脚本转化成sqlite数据库脚本的修改点

转换数据类型 将MySQL的数据类型转换为SQLite对应的数据类型。例如&#xff0c;将 INT或 INTEGER 转换为 INTEGER&#xff0c;将 VARCHAR、TEXT 或 CHAR 转换为 TEXT&#xff0c;将 DATETIME 或 TIMESTAMP 转换为 TEXT 或 DATETIME&#xff08;SQLite没有专门的日期时间类型&am…

Java_Day06学习

抽象类 1.定义&#xff1a;Java也可以创建一种类专门用来当作父类&#xff0c;这种类称为“抽象类”&#xff1b; //抽象类的作用有点类似“模版”&#xff0c;其目的是要设计者依据它的格式来修改并创建新的类。但是并不能直接由抽象类创建对象&#xff0c;只能通过抽象类派生…

JSP(Java Server Pages)基础使用二

简单练习在jsp页面上输出出乘法口诀表 既然大家都是来看这种代码的人了&#xff0c;那么这种输出乘法口诀表的这种简单算法肯定是难不住大家了&#xff0c;所以这次主要是来说jsp的使用格式问题。 <%--Created by IntelliJ IDEA.User: ***Date: 2024/7/18Time: 11:26To ch…

Python Web 与物联网(IoT)集成与实时数据处理

Python Web 与物联网&#xff08;IoT&#xff09;集成与实时数据处理 目录 &#x1f310; IoT 与 Python 的集成&#x1f4e1; 使用 Flask/FastAPI 构建 IoT 中的 Web 接口与控制面板&#x1f517; 使用 MQTT 协议与 Paho 库进行设备间通信&#x1f5c4;️ 在 Python 中处理传…