当前位置: 首页 > 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();//隐藏加载中

}

相关文章:

  • Python 面向对象编程基础
  • CSR、SSR、SSG
  • 相关数据库类型介绍
  • 如果MySQL已经安装但mysql --version命令不好用,怎么办?
  • MySQL索引详解
  • Chrome截取网页全屏
  • The First项目报告:探索Yield Guild Games运行机制与发展潜力
  • 【SQL】产品分组销售
  • 基于php的在线租房管理系统
  • 分享课程:VUE数据可视化教程
  • kubernetes基础配置(入门操作)
  • Android平台Unity3D下如何同时播放多路RTMP|RTSP流?
  • 项目集成SpringSecurity框架
  • vue 项目中的配置文件(.env)的用法
  • 北斗三号多模对讲机TD70:公专网融合、数模一体、音视频调度,推动应急通信效能升级
  • 【Amaple教程】5. 插件
  • Android 架构优化~MVP 架构改造
  • Asm.js的简单介绍
  • ES10 特性的完整指南
  • java中具有继承关系的类及其对象初始化顺序
  • Python十分钟制作属于你自己的个性logo
  • 聊聊directory traversal attack
  • 普通函数和构造函数的区别
  • 前端面试题总结
  • 前端自动化解决方案
  • 深度解析利用ES6进行Promise封装总结
  • 实现菜单下拉伸展折叠效果demo
  • 探索 JS 中的模块化
  • 我的业余项目总结
  • 小程序 setData 学问多
  • 移动端唤起键盘时取消position:fixed定位
  • 优化 Vue 项目编译文件大小
  • ​ 轻量应用服务器:亚马逊云科技打造全球领先的云计算解决方案
  • #13 yum、编译安装与sed命令的使用
  • #laravel 通过手动安装依赖PHPExcel#
  • (1/2) 为了理解 UWP 的启动流程,我从零开始创建了一个 UWP 程序
  • (13)[Xamarin.Android] 不同分辨率下的图片使用概论
  • (70min)字节暑假实习二面(已挂)
  • (附源码)ssm本科教学合格评估管理系统 毕业设计 180916
  • (黑马点评)二、短信登录功能实现
  • (四)搭建容器云管理平台笔记—安装ETCD(不使用证书)
  • (四)七种元启发算法(DBO、LO、SWO、COA、LSO、KOA、GRO)求解无人机路径规划MATLAB
  • (五)关系数据库标准语言SQL
  • (一)Docker基本介绍
  • (原創) 如何讓IE7按第二次Ctrl + Tab時,回到原來的索引標籤? (Web) (IE) (OS) (Windows)...
  • (原創) 如何刪除Windows Live Writer留在本機的文章? (Web) (Windows Live Writer)
  • (转)程序员疫苗:代码注入
  • (转)从零实现3D图像引擎:(8)参数化直线与3D平面函数库
  • (转)关于pipe()的详细解析
  • (转)用.Net的File控件上传文件的解决方案
  • ***php进行支付宝开发中return_url和notify_url的区别分析
  • ***详解账号泄露:全球约1亿用户已泄露
  • .Mobi域名介绍
  • .net core 微服务_.NET Core 3.0中用 Code-First 方式创建 gRPC 服务与客户端
  • .NET Standard 的管理策略