axios怎么获取请求时间

要做一个加载进度条,请问axios,可以拿到它请求的时间吗,或者有什么简便的方法实现

阅读 10.9k
2 个回答

进度条一般都是假的....
请求过程中,时间都是未知的,怎么确定百分比呢?
Axios.interceptors.request.useconsole.log(+new Date())不就是请求时间吗?放进store里。
Axios.interceptors.response.use里再获取结束时间。
但是都结束了,拿到时间似乎也没啥卵用...而你要做的是请求的过程中显示百分比....

可以试下这个

vue-progressbar

axios可以获取上传或下载进度的,请看看请求配置下的这两个方法:
request config

onUploadProgress: function (progressEvent) {
    // Do whatever you want with the native progress event
  },

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

我试过是可以的,以前写的代码:

this.axios({
          method: 'POST',
          url: this.url,
          data: fmData,
          params: this.params,
          transformRequest: function(data) {
            return data
          },
          onUploadProgress: function(progressEvent) {
            vm.$nextTick(function() {
              if (progressEvent.total === 0) {
                vm.percentage = 0
              } else {
                vm.percentage = Math.round(
                  progressEvent.loaded * 100 / progressEvent.total
                )
              }
            })
          }
        })
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题