当前位置:首页 » 《我的小黑屋》 » 正文

vant的Loading加载动画组件的使用,通过接口拿数据时显示加载状态

18 人参与  2024年03月23日 10:55  分类 : 《我的小黑屋》  评论

点击全文阅读


在store.js中使用vuex全局控制loading显示与隐藏

import Vue from 'vue'import Vuex from 'vuex'Vue.use(Vuex)export default new Vuex.Store({  state: {    LOADING: false  },  mutations: {    showLoading(state) {      state.LOADING = true    },    hideLoading(state) {      state.LOADING = false    }  }

新建loading公共组件页面

<template>  <div class="loading">    <van-loading type="spinner" color="#1989fa" />  </div></template><script>  import Vue from 'vue';  import {Loading} from 'vant';  Vue.use(Loading);  export default {    name: 'LOADING',    data() {      return {}    },  }</script><style lang="scss" scoped>  .loading {    position: fixed;    z-index: 9999;    width: 100%;    height: 100%;    background: rgba(255, 255, 255, 0.5);    display: flex;    justify-content: center;    align-items: center;  }</style>

在App.vue中,将loading组件挂载到工程根节点

挂载到App.vue中之后所有的接口请求都会加载loading组件

可以在需要的页面单独引用

<template>  <div id="app">    <Loading v-show="LOADING"></Loading>    ......//其他代码  </div></template><script>  import {mapState} from 'vuex'  import Loading from '@c/Loading.vue'  export default {  // ...解构,将store中的state进行映射。  // 在template中可以直接使用,不需要通过this.$store.state一个个获取store中的state数据。    computed: {      ...mapState(['LOADING'])    },    name: 'App',    components: {Loading}  }</script>

在请求拦截器和响应拦截器中配置

在封装好的axios中,利用axios的拦截器实现请求时提交store显示loading;

请求失败或者完成提交store隐藏loading。

import Vue from "vue";import axios from 'axios';import store from '../../store';// 请求拦截器axios.interceptors.request.use(function (config) {  store.commit('showLoading')  return config;}, function (error) {  store.commit('hideLoading')  return Promise.reject(error);});//响应拦截器axios.interceptors.response.use((response) => {  store.commit('hideLoading')  return response.data;}, (error) => {  store.commit('hideLoading')  return Promise.reject(error);});//绑定到vue原型中Vue.prototype.$http = axios;

如果在单个请求中使用

// 在请求时this.$store.commit('showLoading')//请求完成后  this.$store.commit('hideLoading')


点击全文阅读


本文链接:http://zhangshiyu.com/post/84135.html

<< 上一篇 下一篇 >>

  • 评论(0)
  • 赞助本站

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

最新文章

  • 「太荒混沌鼎」后续更新_「叶无涯柳如烟」小说后续在线免费阅读
  • 全文消失的老婆(昭昭王力)列表_全文消失的老婆
  • (番外)+(全书)爱占便宜的表嫂全书+后续+结局(谢一瑶袁博文)_爱占便宜的表嫂全书+后续+结局列表_笔趣阁(谢一瑶袁博文)
  • 完结文沈青禾贺临渊霍沉洲前言+后续+结局列表_完结文沈青禾贺临渊霍沉洲前言+后续+结局
  • [太子假死逼我学乖,我直接登基了]爆款小说高能章节试读_娇娇李寻太子妃独家章节限时试读
  • 浮云散尽见天晴全书+后续+结局(陆子期苏晚月)全书免费浮云散尽见天晴全书+后续+结局读_列表_笔趣阁(陆子期苏晚月)
  • 全书浏览错综复杂结局+番外(秦封孤以晴)_错综复杂结局+番外(秦封孤以晴)全书结局
  • 故人心易变免费(萧衡梨娘)_故人心易变免费萧衡梨娘
  • 任他明月下西楼无弹窗阅读_[云溪邵淮烬祁雪芷]小说无删减版在线阅读
  • 此去经年人未还(沈青禾霍沉洲)全书浏览_此去经年人未还全书浏览
  • 当女配看见弹幕后,被病娇男主强制爱了+后续+结局(裴纪洲江柠)列表_当女配看见弹幕后,被病娇男主强制爱了+后续+结局(裴纪洲江柠)当女配看见弹幕后,被病娇男主强制爱了+后续+结局在线
  • 完结文此去经年人未还在线看列表_完结文此去经年人未还在线看(霍沉洲沈青禾)

    关于我们 | 我要投稿 | 免责申明

    Copyright © 2020-2022 ZhangShiYu.com Rights Reserved.豫ICP备2022013469号-1