zoukankan      html  css  js  c++  java
  • react项目全局loading实现

    最近在做一个后台管理系统供内部人员使用,交互性没有那么强,所以对于每个页面请求时要加loading,感觉要写的比较麻烦,然后去百度了下能不能全局实现loading:

    需要loading的请求发出后,让loading显示,请求后无论成功还是失败就让loading关闭

    大概代码如下:

    const { request } = require("http")
    
    // 当前正在请求的数量
    let requestCount = 0
    
    // 显示loading
    const showLoading = () => {
      if (requestCount === 0) { // 解决一个页面多个接口请求需要loading
        const dom = document.createElement('div')
        dom.setAttribute('id', 'loading')
        document.body.appendChild(dom)
        ReactDOM.render(<Spin tip="加载中" size="large" />, dom)
      }
      requestCount ++
    }
    
    // 隐藏loading
    const hideLoading = () => {
      requestCount --
      if (requestCount === 0) {
        document.body.removeChild(document.getElementById('loading'))
      }
    }
    
    // eg:
    if (options?.headers?.isLoading) {
        showLoading()
    }
    fetch().then(() => {
        if (options?.headers?.isLoading) {
            hideLoading()
        }
        // DO SOME
    }).catch(() => {
        if (options?.headers?.isLoading) {
            hideLoading()
        }
    })
    
    // 外部调用
    request(url, { headers: { isLoading: true } })
  • 相关阅读:
    用户行为分析之实时数据采集
    用户行为分析之离线数据采集
    RDDs之combineByKey()
    KeyValue对RDDs
    RDD基本操作之Action
    RDDs基本操作之Transformations
    Spark学习之Scala的基础知识
    Spark学习之RDDs介绍
    查看mysql中的用户和密码
    mysql-connector-java-5.-bin.jar 下载方法
  • 原文地址:https://www.cnblogs.com/yxfboke/p/14263828.html
Copyright © 2011-2022 走看看