axiso取消单独请求接口

直接上代码

import axios from 'axios'
const CancelToken = axios.CancelToken
// 上传函数
uploadFile(file) {
  console.log('uploadFile', file)
  const formData = new FormData()
  formData.append('file', file.file)
  const CancelToken = axios.CancelToken
  fileUpload(formData, {
    onUploadProgress: this.onUploadProgress(file),
    cancelToken: new CancelToken(c => { this.cancel[file.file.uid] = c }) // 实例化一个CancelToken对象,赋值cancel方法
  }).then(res => {
    if (res.data.code == 200) {
      file.onSuccess(res.data.data)
    } else {
      file.onError(res.data.msg)
    }
  }).catch(err => {
    file.onError(err)
  })
},

// api文件中的函数
export const fileUpload = (params, config) =>
  request({
    url: '/api/blade-resource/oss/endpoint/put-file-attach',
    method: 'post',
    headers: {
      'Content-Type': 'multipart/form-data'
    },
    timeout: 20 * 60 * 1000,
    data: params,
    ...config
  })

参考网址open in new window

Last Updated:
Contributors: haizhiguang