了解深圳网站定制开发,南通做网站需要多少钱,基于python的网站开发项目,wordpress加载条插件下载Kapture 2018-06-07 at 14.57.40.gif背景业务需求是这样子的#xff0c;每当发请求到后端时就触发一个全屏的 loading#xff0c;多个请求合并为一次 loading。现在项目中用的是 vue 、axios、element等#xff0c;所以文章主要是讲如果使用 axios 和 element 实现这个功能。…Kapture 2018-06-07 at 14.57.40.gif背景业务需求是这样子的每当发请求到后端时就触发一个全屏的 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 0export function showFullScreenLoading() {if (needLoadingRequestCount 0) {startLoading()}needLoadingRequestCount}export function tryHideFullScreenLoading() {if (needLoadingRequestCount 0) returnneedLoadingRequestCount--if (needLoadingRequestCount 0) {endLoading()}}startLoading()和endLoading()就是调用 element 的 loading 方法。import { Loading } from element-uilet loadingfunction 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 合并什么意思呢请看下图image.png在 request1 的 loading 还没结束时request2 的 loading 已经开始。这种情况 request1 和 request2 在时间上有一定的重合所以 loading 可以合并。那么 request3 是在 request2 结束后 100ms 开始 loading.这时你会发现 loading 两次并且中间有一次极短的闪烁这当然是很不好的体验了。我们只需要修改 tryHideFullScreenLoading 即可export function tryHideFullScreenLoading() {if (needLoadingRequestCount 0) returnneedLoadingRequestCount--if (needLoadingRequestCount 0) {_.debounce(tryCloseLoading, 300)()}}const tryCloseLoading () {if (needLoadingRequestCount 0) {loading.close()}}在之前的版本中tryHideFullScreenLoading 方法会判断 needLoadingRequestCount 0 立即关闭 loading。现在使用 lodash. debounce,延迟 300ms 再调用 tryCloseLoading 方法。现在 300ms 间隔内的 loading 也就合并为一次啦……