当前位置: 首页 > news >正文

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

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

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.lryc.cn/news/447768.html

相关文章:

  • 基于SSM+小程序的在线课堂微信管理系统(在线课堂1)(源码+sql脚本+视频导入教程+文档)
  • Uniapp 微信小程序 最新 获取用户头像 和 昵称 方法 有效可用
  • 儿童手抄报模板-200个(家有神兽必备)
  • 动态规划入门题目->使用最小费用爬楼梯
  • 中间添加一条可以拖拽的分界线,来动态调整两个模块的宽度
  • C++的vector优化
  • 基于飞腾平台的OpenCV的编译与安装
  • pyside6与协程
  • 手机如何五开玩梦幻西游端游?用GameViewer远程手机免费畅玩梦幻西游
  • 【笔记】X射线物理基础
  • Vue3与Flask后端Demo
  • 第一本RAG书籍《大模型RAG实战》出版!
  • Pandas -----------------------基础知识(四)
  • 鼎阳加油-IOC关键技术问题的解决记
  • 【HarmonyOS】TaskPool非阻塞UI
  • 关于使用/bin/sh -c 用于Dockerfile的Entrypoint的问题
  • JS---获取浏览器可视窗口的尺寸
  • 对抗攻击方法详解:梯度攻击、转移攻击与模型集成攻击
  • GPU并行效率问题——通过MPS提升GPU计算收益
  • patch 命令:补丁的应用
  • 仓颉编程语言4,遇到BUG求助
  • SpringIOCDI
  • 单细胞Seruat和h5ad数据格式互换(R与python)方法学习和整理
  • 分布式难题-三座大山NPC
  • 两个方法教你设置Excel密码,防止修改和复制Excel表格内容
  • Java解析Excel文件
  • Require:基于雪花算法完成一个局部随机,全局离散没有热点切唯一的数值Id生成器。
  • libevent - Macro function
  • 408算法题leetcode--第17天
  • 机器人顶刊IEEE T-RO发布无人机动态环境高效表征成果:基于粒子的动态环境连续占有地图