axios取消请求

Axios 使用 cancel token 取消请求

1、先在axios请求中加上cancelToken

复制代码
import request from '../utils/request' // 配置过的Axios 对象
import axios from 'axios' 

export function getDetail(params, that) { 
  return request({
    url: '/api/indexlineage/detail', 
    method: 'get',
    params: params,
    cancelToken: new axios.CancelToken(function executor(c) { // 设置 cancel token
      that.source = c;
    })
  })
}

2、在组件中调用

复制代码
import { getDetail} from "../api";
export default {
    data() {
      return {
        source: null
      }
    },
    methods: {
      cancelQuest() {
        if (typeof this.source === 'function') {
          this.source('终止请求'); //取消请求
        }
      },
      getDetailData(val) {
        this.cancelQuest() // 请求发送前调用
        let params = {
            id: '1',
        }
        getDetail(params, this).then(res => {
              console.log(res)
            }
        )
    }
}
相关推荐
土豆~1 分钟前
文件名没后缀就打不开:前端文件预览的内容嗅探改造
前端·状态模式
quantdash_cc1 分钟前
告别自建 Requests/BS4 网页爬虫:基于 QuantDash 搭建零维保的高性能量化行情流水线
开发语言·爬虫·python·pandas·量化·quantdash
ydd1001008 分钟前
寻找两个正序数组的中位数 Java 题解,二分分割详解
java·开发语言
半亩码田38 分钟前
C#转Python第3.1篇:Python 的 class 没有访问修饰符?面向对象的另一条路
开发语言·python·c#
Euato_Key1 小时前
全栈视野学习Cookie——理解 Cookie 的本质
前端
前端开发呀1 小时前
我的 AI 终端配置清单
前端
敲代码的玉米C1 小时前
测试一直在写你的真实数据根
前端·人工智能·架构
Wzx1980121 小时前
python沙箱和docker沙箱你选对了吗?
开发语言·python·docker
jjw_zyfx1 小时前
css vue vite实现闪烁的呼吸效果
javascript·css·vue.js
执子念的飞鱼1 小时前
File Viewer 进入 2K 节点后,我更在意这 3 条回归
前端·typescript