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

news/2024/9/25 8:05:24/

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

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/news/1530194.html

相关文章

自然语言处理实战项目:从理论到实现

一、引言 自然语言处理&#xff08;NLP&#xff09;是计算机科学、人工智能和语言学交叉的领域&#xff0c;旨在让计算机能够理解、处理和生成人类语言。随着互联网的飞速发展&#xff0c;大量的文本数据被产生&#xff0c;这为自然语言处理技术的发展提供了丰富的素材&#xf…

node js版本低导致冲突WARN EBADENGINE package: required: { node: ‘>=18‘ }

重新安装依赖包 1、删除旧的 node_modules 目录和 package-lock.json 文件&#xff1a; rm -rf node_modules rm package-lock.json2、升级node版本 wget -qO- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.3/install.sh | bashexport NVM_DIR"$([ -z "${…

关于 Goroutines 和并发控制的 Golang 难题

下面是一道关于 Goroutines 和并发控制的 Golang 难题&#xff0c;它涉及到 Go 的并发编程模型、Goroutines、通道&#xff08;Channels&#xff09;以及 sync.WaitGroup 的使用&#xff1a; 问题描述&#xff1a; 你有一个需要并发执行的任务&#xff0c;其中有 100 个 URL …

【赵渝强老师】K8s中的Deployment控制器

K8s的Deployment将Pod部署成无状态的应用程序&#xff0c;它只关心Pod的数量、Pod更新方式、使用的镜像和资源限制等。由于是无状态的管理方式&#xff0c;因此Deployment中没有角色和顺序的概念&#xff0c;换句话说&#xff1a;Deployment中没有状态。   通过使用Deploymen…

研究生第一次刷力扣day1

1.给定一个整数数组 nums 和一个整数目标值 target&#xff0c;请你在该数组中找出和为目标值target 的那两个整数&#xff0c;并返回它们的数组下标 直接采用暴力求解&#xff0c;其他解答案看不懂 大致思想&#xff1a;先用len函数求出数组的长度n&#xff0c;然后一个个遍…

【大数据入门 | Hive】DDL数据定义语言(数据库DataBase)

1. 数据库(DataBase) 1.1 创建数据库 语法&#xff1a; CREATE DATABASE [IF NOT EXISTS] database_name [COMMENT database_comment] [LOCATION hdfs_path] [WITH DBPROPERTIES (property_nameproperty_value, ...)]; 案例&#xff1a; &#xff08;1&#xff09;创建一个…

Linux Shell: 使用 Expect 自动化 SCP 和 SSH 连接的 Shell 脚本详解

文章目录 0. 引言2. 解决方案3. 脚本详解脚本1&#xff1a;使用 SSH 和 Expect 自动化登录远端机器脚本说明 脚本2&#xff1a;使用 SCP 和 Expect 自动化文件上传脚本说明 脚本3&#xff1a;使用 SCP 和 Expect 自动化文件下载脚本说明 4. 脚本的使用方法5. 关键技术点5.1. Ex…

PhpStudy | PHP 版本切换流程

关注这个软件的其他相关笔记&#xff1a;PhpStudy —— README-CSDN博客 在使用多样化的 PHP Web 应用程序时&#xff0c;选择合适的 PHP 版本至关重要。例如&#xff0c;一些老旧的应用程序可能是基于早期版本的 PHP 开发的&#xff0c;如果使用最新版本的 PHP 来运行&#xf…