美文网首页web前端开发
Axios的config如何配置

Axios的config如何配置

作者: 一个废人 | 来源:发表于2017-10-27 16:05 被阅读0次

    https://github.com/axios/axios#request-config

     // `onUploadProgress` allows handling of progress events for uploads
      onUploadProgress: function (progressEvent) {
        // Do whatever you want with the native progress event
      },
    

    工作中用到的是利用一个callback函数拿到上传时的进度.

    FileUploader.vue

           let callback = (progress)=>{
              self.progress = progress
            }
            upload(formData,callback).then((response)=>{
              console.log(response)
            }).catch((err)=>{
            })
    

    api.js

    export function upload(file,callback) {
      const url = `${BASE_URL}/api/upload`;
      let config = {
        headers: {
          'Content-Type': 'multipart/form-data',
        },
        onUploadProgress: function(progressEvent) {
          let percentCompleted = Math.round( (progressEvent.loaded * 100) / progressEvent.total );
          callback(percentCompleted)
        }
      }
      return axios.post(url,file,config);
    }
    

    相关文章

      网友评论

        本文标题:Axios的config如何配置

        本文链接:https://www.haomeiwen.com/subject/zlfdpxtx.html