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

该文章介绍了在uniapp项目中创建自定义loding组件,并通过vuex进行全局状态管理,包括在main.js中挂载store,配置loding状态,接口请求时动态设置loding状态,以及在页面中引用loding组件的方法。
摘要由CSDN通过智能技术生成

一、在项目中创建loding组件

在uniapp的components文件夹下创建loding组件,如图:
在这里插入图片描述

示例代码:

<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

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

const app = {
	state: {
		loding: false,
	},

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

	},

	actions: {

	}
}

export default app

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

import loding from './loding'

export default {
  login,
}

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

const getters = {
	loding: state => state.my.loding, //loding
}


export default getters

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

import {createStore} from 'vuex'

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

export default new createStore({
  modules,
  state: {

  },
  mutations: {

  },
  actions: {

  },
  getters
})

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

const getters = {
	loding: state => state.my.loding, //loding
}


export default getters

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

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

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

<template>
<!-- 引用loding -->
	<xc-loading v-if="this.$store.getters.loding"></xc-loading>
</template>
  • 3
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

smileAgain-lg

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值