用Axios Element 实现全局的请求 loading


demo in github
背景
业务需求是这样子的,每当发请求到后端时就触发一个全屏的 loading,多个请求合并为一次 loading。
现在项目中用的是 vue 、axios、element等,所以文章主要是讲如果使用 axios 和 element 实现这个功能。
分析
首先,请求开始的时候开始 loading, 然后在请求返回后结束 loading。重点就是要拦截请求和响应。
然后,要解决多个请求合并为一次 loading。
最后,调用element 的 loading 组件即可。
拦截请求和响应
axios 的基本使用方法不赘述。笔者在项目中使用 axios 是以创建实例的方式。
// 创建axios实例
const $ = axios.create({
baseURL: `${URL_PREFIX}`,
timeout: 15000
})
然后再封装 post 请求(以 post 为例)
export default {
post: (url, data, config = { showLoading: true }) => $.post(url, data, config)
}
axios 提供了请求拦截和响应拦截的接口,每次请求都会调用showFullScreenLoading
方法,每次响应都会调用tryHideFullScreenLoading()
方法
// 请求拦截器
$.interceptors.request.use((config) => {
showFullScreenLoading()
return config
}, (error) => {
return Promise.reject(error)
})
// 响应拦截器
$.interceptors.response.use((response) => {
tryHideFullScreenLoading()
return response
}, (error) => {
return Promise.reject(error)
})
那么showFullScreenLoading
tryHideFullScreenLoading()
要干的事儿就是将同一时刻的请求合并。声明一个变量needLoadingRequestCount
,每次调用showFullScreenLoading
方法 needLoadingRequestCount + 1
。调用tryHideFullScreenLoading()
方法,needLoadingRequestCount - 1
。needLoadingRequestCount
为 0 时,结束 loading。
let needLoadingRequestCount = 0
export function showFullScreenLoading() {
if (needLoadingRequestCount === 0) {
startLoading()
}
needLoadingRequestCount++
}
export function tryHideFullScreenLoading() {
if (needLoadingRequestCount <= 0) return
needLoadingRequestCount--
if (needLoadingRequestCount === 0) {
endLoading()
}
}
startLoading()
和endLoading()
就是调用 element 的 loading 方法。
import { Loading } from 'element-ui'
let loading
function startLoading() {
loading = Loading.service({
lock: true,
text: '加载中……',
background: 'rgba(0, 0, 0, 0.7)'
})
}
function endLoading() {
loading.close()
}
到这里,基本功能已经实现了。每发一个 post 请求,都会显示全屏 loading。同一时刻的多个请求合并为一次 loading,在所有响应都返回后,结束 loading。
功能增强
实际上,现在的功能还差一点。如果某个请求不需要 loading 呢,那么发请求的时候加个 showLoading: false
的参数就好了。在请求拦截和响应拦截时判断下该请求是否需要loading,需要 loading 再去调用showFullScreenLoading()
方法即可。
在封装 post 请求时,已经在第三个参数加了 config 对象。config 里包含了 showloading
。然后在拦截器中分别处理。
// 请求拦截器
$.interceptors.request.use((config) => {
if (config.showLoading) {
showFullScreenLoading()
}
return config
})
// 响应拦截器
$.interceptors.response.use((response) => {
if (response.config.showLoading) {
tryHideFullScreenLoading()
}
return response
})
我们在调用 axios 时把 config 放在第三个参数中,axios 会直接把 showloading 放在请求拦截器的回调参数里,可以直接使用。在响应拦截器中的回调参数 response 中则是有一个 config 的 key。这个 config 则是和请求拦截器的回调参数 config 一样。
更新:2018-6-7
合并一定间隔时间内请求的 loading
上面的代码已经实现了将有时间重合的 loading 合并,什么意思呢?请看下图

在 request1 的 loading 还没结束时,request2 的 loading 已经开始。这种情况 request1 和 request2 在时间上有一定的重合,所以 loading 可以合并。
那么 request3 是在 request2 结束后 100ms 开始 loading.这时你会发现 loading 两次,并且中间有一次极短的闪烁,这当然是很不好的体验了。
我们只需要修改 tryHideFullScreenLoading 即可:
export function tryHideFullScreenLoading() {
if (needLoadingRequestCount <= 0) return
needLoadingRequestCount--
if (needLoadingRequestCount === 0) {
_.debounce(tryCloseLoading, 300)()
}
}
const tryCloseLoading = () => {
if (needLoadingRequestCount === 0) {
loading.close()
}
}
在之前的版本中,tryHideFullScreenLoading 方法会判断 needLoadingRequestCount === 0 立即关闭 loading。现在使用 lodash. debounce,延迟 300ms 再调用 tryCloseLoading 方法。
现在 300ms 间隔内的 loading 也就合并为一次啦……
用Axios Element 实现全局的请求 loading的更多相关文章
- AngularJS 拦截器实现全局$http请求loading效果
日常项目开发中,当前端需要和后端进行数据交互时,为了友好的UI效果,一般都会在前端加个loading的状态提示(包括进度条或者icon显示),数据传输或交互完成之后,再隐藏/删除loading提示. ...
- AXIOS构建请求处理全局loading状态&&AXIOS避免重复请求loading多次出现
一般情况下,在 vue 中结合 axios 的拦截器控制 loading 展示和关闭,是这样的:在 App.vue 配置一个全局 loading. <div class="app&qu ...
- Axios使用拦截器全局处理请求重试
Axios拦截器 Axios提供了拦截器的接口,让我们能够全局处理请求和响应.Axios拦截器会在Promise的then和catch调用前拦截到. 请求拦截示例 axios.interceptors ...
- 前端MVC Vue2学习总结(六)——axios与跨域HTTP请求、Lodash工具库
一.axios Vue更新到2.0之后宣告不再对vue-resource更新,推荐使用axios,axios是一个用于客户端与服务器通信的组件,axios 是一个基于Promise 用于浏览器和 no ...
- React、Vue添加全局的请求进度条(nprogress)
全局的请求进度条,我们可以使用nprogress来实现,效果如下: 首先需要安装插件: npm i nprogress -S 然后使用的时候主要有两种方式,第一种是切换页面的时候,第二种则是请求接口的 ...
- vue2 自定义全局组件(Loading加载效果)
vue2 自定义全局组件(Loading加载效果) github地址: https://github.com/ccyinghua/custom-global-component 一.构建项目 vue ...
- axios 是如何封装 HTTP 请求的
原载于 TutorialDocs 网站的文章<How to Implement an HTTP Request Library with Axios>.译者:zhangbao90shttp ...
- Jquery Ajax简单封装(集中错误、请求loading处理)
Jquery Ajax简单封装(集中错误.请求loading处理) 对Jquery Ajax做了简单封装,错误处理,请求loading等,运用到项目中集中处理会很方便. 技术层面没有什么好说的,请求是 ...
- FastAPI实践项目:SayHello(FastAPI + vue.js + axios + element ui)
目录 简介 翻版 VS 本尊 后端服务 源码 接下来 简介 这次带来的是FastAPI + vue.js + axios + element ui (一个html文件里使用的) 实现的<Flas ...
随机推荐
- Jedis使用管道优化批量输出插入的效率
Jedis连接池: package com.daxin.jedis_datastructure; /** * * @author daxin * * @email leodaxin@163com * ...
- esp8266(3) Arduino通过ESP8266连接和获取网站源代码
http://www.plclive.com/a/tongxinjiekou/2016/0422/374.html 在上一篇8266的基础上,这一篇做个具体的连接网站的例子,供大家参考.上一篇基础篇请 ...
- Linux中运行SpringBoot项目,永久运行
将写好的springboot项目打成jar包: 项目右键 -- Run As -- Maven build... ---此时出现下图 1.Goals 中填写:install 2.Skip Tests复 ...
- 蒟蒻qxt的sd'日常
emm... 今天刷了一道水题 居然 死循环了 真的是水题啊 顿时自闭 (救救孩子吧) 结果 bug是 我把for循环中的i的值改变了 使得i的值一直都不会达到他的边界值 于是就死循环了 所以 要用到 ...
- matlab sign函数用法及实例
在MATLAB科学计算过程当中,我们经常需要对我们的计算公式或者计算结果检验其符号,,sign函数就给我们提供了这种方便,下面就通过实例介绍一下matlab sign函数 的用法,希望能够给您带来帮助 ...
- 深入浅出的webpack构建工具---PostCss(五)
一:PostCss是什么? PostCss是一个样式处理工具,它通过自定义的插件和工具生态体系来重新定义css.它鼓励开发者使用规范的css原生语法编写代码,然后配置编译器转换需要兼容的浏览器版本, ...
- AI 正则化
正则化,是减少泛化误差的技术.
- hibernate4使用原生jdbc进行批处理
在hibernate中,有一级缓存session和二级缓存sessionFactory这些机制,一方面为编码提供了便利,同时也会有一些副作用.比如有较大的数据量交互的话,缓存反而会降低效率.最近在做一 ...
- SkylineGlobe 6.5 如何实现简单多边形的动态绘制 C#示例代码
在Skyline的TEPro软件中,我们可以很容易地绘制出多边形. 那么,在二次开发过程中,该如何绘制一个简单的多边形呢? 通过下面的示例代码,我们可以很容易完成这一项工作. 其中,重点需要了解Geo ...
- Redis详解(六)------ RDB 持久化
前面我们说过,Redis 相对于 Memcache 等其他的缓存产品,有一个比较明显的优势就是 Redis 不仅仅支持简单的key-value类型的数据,同时还提供list,set,zset,has ...