使用vuex在vue3.0 + vant 项目中的 loading加载

版本:
"vue": "^3.0.5",
"vuex": "^4.0.0"

vue2.x版本(使用vuex在vue2.x + vant 项目中加 loading加载

在src目录下新建store目录,store目录下新建index.js文件  (已确保已装好vuex)

import {createStore} from "vuex";

export default createStore({
    state: {
        loading: false
    },
    mutations: {
        showLoading(state){
            state.loading = true    
        },
        hideLoading (state) {
            state.loading = false
        }},
    actions: {},
    modules: {}
});

loading组件如下:(图片建议网上找无背景的图)附我用的图(http://img.lanrentuku.com/img/allimg/1212/5-121204193R0-50.gif)或 使用vant中loading 加载(详细属性见vant中loading)

<template>
  <div class="loading">
    <img src="./../assets/img/loading.gif" alt="">
    <van-loading class="loadingVant" vertical type="spinner">加载中...</van-loading>
  </div>
</template>

<script lang="ts">
    import {defineComponent, toRefs, computed, ref, reactive, onMounted} from 'vue';

    export default defineComponent({
        name: "",
        components: {},
        setup() {
            const reactiveData = reactive({});

            onMounted(() => {

            });

            return {
                ...toRefs(reactiveData),
            };
        },
    });
</script>
<style scoped>
  .loading{
    position: fixed;
    top:0;
    left:0;
    z-index:121;
    width: 100%;
    height: 100%;
    background: rgba(0,0,0,0.3);
    display: table-cell;
    vertical-align: middle;
    text-align: center;
  }
  .loading img{
  	width: 1rem;
  	height: 1rem;
    margin:7.5rem auto;
  }
  .loadingVant {    
     margin:310px auto
   }
</style>

在app.vue中使用loading组件

<template>
  <div id="app">
  	<loading v-show="loading"></loading>
    <router-view/>
  </div>
</template>

<script>
    import {useStore} from 'vuex'
	import Loading from '@/components/loading.vue'
	import {defineComponent, computed,} from 'vue';

	export default defineComponent({
		name: 'App',
		components: {
			Loading,
		},
		setup() {
			const store = useStore()
			return {
				loading: computed(() => store.state.loading)
			}
		}
	})
</script>

<style>
#app {
  font-family: Helvetica, sans-serif;
  height: 100%;
}
</style>

然后main.js

import store from './store'


const app = createApp(App);
app
    .use(Vant)
    .use(store)
    .use(router).mount('#app')

如果需要在请求封装中使用在请求中加

import {useStore} from 'vuex' 

const store = useStore()

axios.interceptors.request.use(
    config => {
        store.commit('showLoading')
    }
    error => {
        store.commit('hideLoading')
    }
)
axios.interceptors.response.use(
    response => {
        store.commit('hideLoading')
    },

    error => {
        store.commit('hideLoading')
    }
);

如在单个请求中使用,在 请求时 中加

 store.commit('showLoading')
       
  请求完成后加    
  store.commit('hideLoading')
 

有问题可以评论,有时间必回

  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值