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)
            }
        )
    }
}
相关推荐
flytam2 分钟前
Claude Agent SDK 深度入门指南
前端·aigc·agent
weixin1997010801616 分钟前
《电天下商品详情页前端性能优化实战》
前端·性能优化
速易达网络17 分钟前
vue+echarts开发的图书数字大屏系统
前端
LJianK125 分钟前
Java中的类、普通类,抽象类,接口的区别
java·开发语言
Dev7z25 分钟前
基于MATLAB的5G物理层文本传输系统仿真与性能分析
开发语言·5g·matlab
小智社群26 分钟前
贝壳获取小区的名称
开发语言·前端·javascript
lsx20240636 分钟前
Python3 OS模块详解
开发语言
Ferries43 分钟前
《从前端到 Agent》系列|03:应用层-RAG(检索增强生成,Retrieval-Augmented Generation)
前端·人工智能·机器学习
Jessica_Lee1 小时前
Openclaw智能体终止机制
javascript
米丘1 小时前
Connect 深度解析:Node.js 中间件框架的基石
javascript·http·node.js