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

embedded/2024/9/29 11:15:53/

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

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/embedded/119315.html

相关文章

WebView is not allowed in privileged processes

1、现象 最近客户提出一个问题&#xff0c;应用使用webview加载网页时&#xff0c;提示“For security reasons, WebView is not allowed in privileged processes”&#xff0c;然后网页无法加载。2、分析 通过该提示&#xff0c;在源码中找到了报出该提示的所在之处。出于安全…

pytorch ----【输入张量.data.size()/输入张量.size()】的使用

import torch test_data torch.randn(1, 12, 32, 32) batchsize, num_channels, height, width test_data.data.size() 在提供的代码中&#xff0c;test_data 是一个形状为 (1, 12, 32, 32) 的随机张量&#xff0c;表示一个批次&#xff08;batch&#xff09;中有 1 张图像&…

Reis数据库及key的操作命令汇总

管理命令 命令含义BGREWRITEAOF异步执行一个 AOF&#xff08;AppendOnly File&#xff09; 文件重写操作BGSAVE在后台异步保存当前数据库的数据到磁盘CLIENT关闭客户端连接CLIENT LIST获取连接到服务器的客户端连接列表CLIENT GETNAME获取连接的名称CLIENT PAUSE在指定时间内终…

npm 源切换以及添加 使用工具 nrm 使用方法

nrm (npm Registry Manager) 是一个用于管理和切换 npm 源的命令行工具&#xff0c;可以帮助开发者更方便地在不同的 npm 源之间切换&#xff0c;特别是对于需要频繁切换源的用户来说非常有用。以下是 nrm 的基本使用方法&#xff1a; 1. 安装 nrm 首先&#xff0c;你需要全局…

【Qt】Qt中的窗口坐标 信号与槽

Qt中的窗口坐标 && 信号与槽 1. Qt中的窗口坐标2. 信号与槽的概述3. 信号和槽的使用3.1 connect函数的使用3.2 查看内置信号和槽3.2 connect的参数类型不匹配问题 4. 自定义信号 && 自定义槽4.1 自定义槽4.2 自定义信号 5. 带参数的信号和槽6. 信号与槽的关联方…

教师工作量|基于springBoot的教师工作量管理系统设计与实现(附项目源码+论文+数据库)

私信或留言即免费送开题报告和任务书&#xff08;可指定任意题目&#xff09; 目录 一、摘要 二、相关技术 三、系统设计 四、数据库设计 五、核心代码 六、论文参考 七、源码获取 一、摘要 传统信息的管理大部分依赖于管理人员的手工登记与管理&#xff…

记录linux环境下搭建本地MQTT服务器实现mqtt的ssl加密通讯

1、ubuntu安装mosquitto sudo apt-get update//安装服务端 sudo apt-get install mosquitto//安装客户端 sudo apt-get install mosquitto-clients 2、安装openssl 3、mqtts/tls加密传输 mosquitto原生支持了TLS加密&#xff0c;TLS&#xff08;传输层安全&#xff09;是SSL&…

基于python+spark的外卖餐饮数据分析系统设计与实现(含论文)-Spark毕业设计选题推荐

博主介绍&#xff1a; 大家好&#xff0c;本人精通Java、Python、C#、C、C编程语言&#xff0c;同时也熟练掌握微信小程序、Php和Android等技术&#xff0c;能够为大家提供全方位的技术支持和交流。 我有丰富的成品Java、Python、C#毕设项目经验&#xff0c;能够为学生提供各类…