V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
shakaraka
0D

如何在 axios 的默认配置里,处理 progress?

  •  
  •   shakaraka ·
    PRO
    · Feb 28, 2018 · 4705 views
    This topic created in 2994 days ago, the information mentioned may be changed or developed.
    import axios from 'axios'
    
    let config =  axios.create({
        withCredentials: true,
        headers:{
            'X-Requested-With': 'XMLHttpRequest'
        },
        onUploadProgress: p => { return 100 * ( p.loaded / p.total ) },
        onDownloadProgress: p => { return 100 * ( p.loaded / p.total ) }
    })
    
    export default config
    
    import axios from 'config'
    axios.post('/喵', {
        onUploadProgress (a){
            console.log(a)
        },
        onDownloadProgress (a){
            console.log(a)
        }
    })
    

    因为我每个 axios 请求都需要进度条

    所以我希望这两个 Progress 返回的都是百分比而不是 Progress 事件对象,不然每个 axios 请求都要写一次处理 function

    应该怎么做啊?

    No Comments Yet
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   5561 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 34ms · UTC 09:15 · PVG 17:15 · LAX 02:15 · JFK 05:15
    ♥ Do have faith in what you're doing.