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)
            }
        )
    }
}
相关推荐
Lysun0011 分钟前
dispaly: inline-flex 和 display: flex 的区别
前端·javascript·css
山禾女鬼00112 分钟前
Vue 3 自定义指令
前端·javascript·vue.js
啊卡无敌13 分钟前
Vue 3 reactive 和 ref 区别及 失去响应性问题
前端·javascript·vue.js
北桥苏18 分钟前
Spine动画教程:皮肤制作
前端
pianmian121 分钟前
arcgis几何与游标(1)
开发语言·python
-曾牛22 分钟前
【LangChain4j快速入门】5分钟用Java玩转GPT-4o-mini,Spring Boot整合实战!| 附源码
java·开发语言·人工智能·spring boot·ai·chatgpt
涵信32 分钟前
第九节:React HooksReact 18+新特性-React 19的use钩子如何简化异步操作?
前端·javascript·react.js
Aaaaaaaaaaayou40 分钟前
浅玩一下 Mobile Use
前端·llm
nanzhuhe40 分钟前
python中参数前**的含义
开发语言·python
这个昵称也不能用吗?41 分钟前
react-native搭建开发环境过程记录
前端·react native·cocoapods