您好,欢迎来到华佗小知识。
搜索
您的当前位置:首页AXIOS构建请求处理全局loading状态&&AXIOS避免重复请求loading多次出现

AXIOS构建请求处理全局loading状态&&AXIOS避免重复请求loading多次出现

来源:华佗小知识

一般情况下,在 vue 中结合 axios 的控制 loading 展示和关闭,是这样的:
在 App.vue 配置一个全局 loading。

    <div class="app"> <keep-alive :include="keepAliveData"> <router-view/> </keep-alive> <div class="loading" v-show="isShowLoading"> <Spin size="large"></Spin> </div> </div>

同时设置 axios 。

 // 添加请求
 this.$axios.interceptors.request.use(config => {
     this.isShowLoading = true return config }, error => { this.isShowLoading = false return Promise.reject(error) }) // 添加响应 this.$axios.interceptors.response.use(response => { this.isShowLoading = false return response }, error => { this.isShowLoading = false return Promise.reject(error) })

这个的功能是在请求前打开 loading,请求结束或出错时关闭 loading。
如果每次只有一个请求,这样运行是没问题的。但同时有多个请求并发,就会有问题了。

举例

假如现在同时发起两个请求,在请求前, this.isShowLoading = true 将 loading 打开。
现在有一个请求结束了。this.isShowLoading = false 关闭 loading,但是另一个请求由于某些原因并没有结束。
造成的后果就是页面请求还没完成,loading 却关闭了,用户会以为页面加载完成了,结果页面不能正常运行,导致用户体验不好。

解决方案
增加一个 loadingCount 变量,用来计算请求的次数。

loadingCount: 0

再增加两个方法,来对 loadingCount 进行增减操作。

    methods: {
        addLoading() {
            this.isShowLoading = true
            this.loadingCount++
        },

        isCloseLoading() {
            this.loadingCount--
            if (this.loadingCount == 0) { this.isShowLoading = false } } }

现在变成这样:

        // 添加请求
        this.$axios.interceptors.request.use(config => {
            this.addLoading() return config }, error => { this.isShowLoading = false this.loadingCount = 0 this.$Message.error('网络异常,请稍后再试') return Promise.reject(error) }) // 添加响应 this.$axios.interceptors.response.use(response => { this.isCloseLoading() return response }, error => { this.isShowLoading = false this.loadingCount = 0 this.$Message.error('网络异常,请稍后再试') return Promise.reject(error) })

这个的功能是:
每当发起一个请求,打开 loading,同时 loadingCount 加1。
每当一个请求结束, loadingCount 减1,并判断 loadingCount 是否为 0,如果为 0,则关闭 loading。
这样即可解决,多个请求下有某个请求提前结束,导致 loading 关闭的问题。

转载于:https://www.cnblogs.com/huancheng/p/11321854.html

因篇幅问题不能全部显示,请点此查看更多更全内容

Copyright © 2019- huatuo0.cn 版权所有 湘ICP备2023017654号-2

违法及侵权请联系:TEL:199 18 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务