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)
            }
        )
    }
}
相关推荐
Swot1 分钟前
SvelteKit Remote Functions 使用指南
前端·后端
Cooper256 分钟前
三源热榜去重加 AI 摘要:URL 唯一键踩出的三个坑
前端·python
Yk酱2284023 分钟前
A2UI探索随笔-技术选型
前端
前端繁华如梦27 分钟前
# 从 73000 行压缩代码到 TypeScript 模块化:我把一个 Three.js FPS 游戏 1:1 复刻了一遍
前端
小厨勺子29 分钟前
我用AIUI给Rokid眼镜做了个随身换算智能体
javascript
Penge66631 分钟前
React Context 详解:从 Props 传递到跨组件状态共享
前端
Penge66640 分钟前
React 事件处理:bind(this) 原理与实际开发常用写法
前端
ZhengEnCi1 小时前
小白用 AI 做官网完全指南:不用框架的六格清单,双击就能打开
前端·html·ai编程
starzy19901 小时前
深入理解Scala隐式值:编译期自动装配
开发语言·后端·scala
zhanlang02171 小时前
HTML 零基础入门到企业实战【第五章 5.2】图片路径精讲:绝对路径、相对路径、网络路径(新手必踩坑点)
前端·html