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();//隐藏加载中

}

相关推荐
waterHBO1 分钟前
css 模拟一个动画效果,消息堆叠。
前端·css
前端加油站17 分钟前
在 cursor 成为流行的开发方式后,作为普通开发我们能做什么
前端
Cache技术分享25 分钟前
163. Java Lambda 表达式 - Function 的链式组合
前端·后端
Zestia41 分钟前
页面点击跳转源代码?——element-jumper插件实现
前端·javascript
前端小白199542 分钟前
面试取经:工程化篇-webpack性能优化之优化loader性能
前端·面试·前端工程化
PineappleCoder42 分钟前
大小写 + 标点全搞定!JS 如何精准统计单词频率?
前端·javascript·算法
zhangbao90s43 分钟前
Web组件:使用Shadow DOM
前端
hhy前端之旅43 分钟前
语义版本控制:掌握版本管理的艺术
前端
coding随想44 分钟前
深入浅出DOM操作的隐藏利器:Range(范围)对象——掌控文档的“手术刀”
前端
前端小白199544 分钟前
面试取经:工程化篇-webpack性能优化之减少模块解析
前端·面试·前端工程化