requestLoading.js 1.25 KB
/*
 * @Description: 定义全局loading
 * @Autor: renchao
 * @LastEditTime: 2023-05-16 14:08:10
 */

import Loading from '@/components/Loading/index.js';
// 定义 loading
let loading

// loading开始 方法
/**
 * @description: loading开始 方法
 * @param {*} loadingText
 * @author: renchao
 */
function startLoading (loadingText = '正在加载中...') {
  loading = Loading.service({
    text: loadingText,
    spinner: 'el-icon-loading',
    background: 'rgba(255, 255, 255, 0.5)',
    target: document.querySelector('.loadingtext')
  })
}
// loading结束 方法
/**
 * @description: loading结束 方法
 * @author: renchao
 */
function endLoading () {
  loading.close()
}
// 定义一个变量,有请求变量加一,收到响应变量减一
let loadingCount = 0

/**
 * 调用一次startLoadingAddCount() 方法 开启Loading 并 loadingCount + 1
 * 调用一次endLoadingSubCount() 方法 loadingCount - 1 直到为0 关闭loading
 */
export function startLoadingAddCount (LoadingText, target) {
  if (loadingCount === 0) {
    startLoading(LoadingText, target)
  }
  loadingCount++
}
/**
 * @description: endLoadingSubCount
 * @author: renchao
 */
export function endLoadingSubCount () {
  loadingCount--
  if (loadingCount === 0) {
    endLoading()
  }
}