vue中axios多次相同请求取消上一个请求

发布时间 2023-08-07 11:35:46作者: Jennyishere

api

import axios from 'axios'; 

export function getDataFlowGraph(id, that) {
  return request({
    requestBase,
    url: `/workflowMetadata/getDataFlowGraph/${id}`,
    method: 'get',
    // 设置 cancel token 用于多次请求,中断上一次请求
    cancelToken: new axios.CancelToken(function executor(c) {
      //传递this增加source属性
      that.source = c
    }),
  })
}

页面请求数据

  data(){
   return{
     source:null
 }
},
....
//请求
     if (this.source) {
        this.source()
      }
      // 发送请求
      this.loading = true
      getDataFlowGraph(id,this).then()