uniapp小程序自定义loding,通过状态管理配置全局使用

一、在项目中创建loding组件

在uniapp的components文件夹下创建loding组件,如图:

示例代码:

c 复制代码
<template>
	<view class="loginLoading">
		<image src="../../static/loading.gif"  class="loading-img" mode=""></image>
	</view>
</template>

<script>
	export default {
		name:"loading",
		data() {
			return {
				
			};
		}
	}
</script>

<style lang="scss" scoped>
	 .loginLoading{
	   width:100%;
	   height:100%;
	   display: flex;
	   left: 0;
	   top: 0;
	   right: 0;
	   bottom: 0;
	   flex-direction: column;
	   position: fixed;
	   justify-content: center;
	   align-items: center;
	   z-index: 999999;
	   .loading-img{
		   width:300rpx;
		   height:110rpx;
	   }
	 }
</style>

二、在main.js中全局挂载store

c 复制代码
import App from './App'
import store from './store'

import { createSSRApp } from 'vue'
import { createI18n } from 'vue-i18n'

const i18n = createI18n(i18nConfig)
export function createApp() {
  const app = createSSRApp(App)
  
  //配置全局属性
  app.config.globalProperties.$store=store;
  // 全局国际化配置
  app.use(i18n)
  return {
    app,
	store,
	created: bootstrap
  }
}

三、配置loding状态管理(状态管理可以按自己的需求配置)

状态管理存储: 在store文件夹下创建modules文件,里面创建loding.js

c 复制代码
const app = {
	state: {
		loding: false,
	},

	mutations: {
		SET_LODING: (state, value) => {
			state.loding = value
		}

	},

	actions: {

	}
}

export default app

模块统一暴露: 在store文件夹下创建modules文件,里面创建index.js

c 复制代码
import loding from './loding'

export default {
  login,
}

获取状态管理: 在store下创建getters.js

c 复制代码
const getters = {
	loding: state => state.my.loding, //loding
}


export default getters

创建状态管理: 在store下创建index.js

c 复制代码
import {createStore} from 'vuex'

import modules from './modules'
import getters from './getters'

export default new createStore({
  modules,
  state: {

  },
  mutations: {

  },
  actions: {

  },
  getters
})

获取状态管理属性值: 在store下创建getters.js

c 复制代码
const getters = {
	loding: state => state.my.loding, //loding
}


export default getters

四、在接口封装中,接口请求开始和接口请求成功,分别设置状态管理loding值为true和false

c 复制代码
// 请求结束
$http.requestEnd = options => {
  // 判断当前接口是否需要加载动画
  if (options.load) {
    requestNum = requestNum - 1
    if (requestNum <= 0) {
      store.commit('SET_LODING', false);
    }
  }
}
c 复制代码
// 请求开始拦截器
$http.requestStart = options => {
  if (options.load) {
    if (requestNum <= 0) {
      // 打开加载动画
     store.commit('SET_LODING', true);
    }
    requestNum += 1
  }
}

五、在页面中引用(因为uniapp无法像H5项目,可以在html中全局引用,所以需要在需要使用loding的页面引用即可),不用在接口中再配置显示隐藏

c 复制代码
<template>
<!-- 引用loding -->
	<xc-loading v-if="this.$store.getters.loding"></xc-loading>
</template>
相关推荐
崔庆才丨静觅14 小时前
hCaptcha 验证码图像识别 API 对接教程
前端
passerby606115 小时前
完成前端时间处理的另一块版图
前端·github·web components
掘了15 小时前
「2025 年终总结」在所有失去的人中,我最怀念我自己
前端·后端·年终总结
崔庆才丨静觅15 小时前
实用免费的 Short URL 短链接 API 对接说明
前端
崔庆才丨静觅15 小时前
5分钟快速搭建 AI 平台并用它赚钱!
前端
崔庆才丨静觅16 小时前
比官方便宜一半以上!Midjourney API 申请及使用
前端
Moment16 小时前
富文本编辑器在 AI 时代为什么这么受欢迎
前端·javascript·后端
崔庆才丨静觅16 小时前
刷屏全网的“nano-banana”API接入指南!0.1元/张量产高清创意图,开发者必藏
前端
剪刀石头布啊16 小时前
jwt介绍
前端
爱敲代码的小鱼16 小时前
AJAX(异步交互的技术来实现从服务端中获取数据):
前端·javascript·ajax