axios 取消请求操作
1.需求背景: 请求操作特别频繁时, 由于触发了多个请求, 不确定哪一个请求能优先回来, 导致 数据不一致
大致效果图
- 核心代码(3种方式, 推荐 AbortController)
- AbortController (Axios 0.22.0 )之后支持
- const controller =
new AbortController()- axios.get('x/xx', {
signal: controller.signal}) 请求配置- 取消操作
controller.abort()- CancelToken(Axios 0.22.0)之后弃用
const CancelToken = axios.CancelTokenconst source = CancelToken.source()- axios.get('x/xx', {
cancelToken: source.token})- 取消操作
source.cancel('Operation canceled by the user')message 可选- CancelToken 构造函数
let cancel = null定义变量const cancelToken =axios.canclToken- axios.get('x/xx',
cancelToken: new CancelToken((c) => cancel = c))- 取消操作 cancel()
代码示例:
<script lang="ts" setup>
import { getCurrentInstance, ref, watch } from 'vue'
import { getNewList } from './api';
import Axios, { Canceler, CancelTokenSource } from 'axios';
const CancelToken = Axios.CancelToken
// 用于取消请求
const testCancel = ref< CancelTokenSource | null>(null)
// 方式1
const sendPost = () => {
try {
// 重复操作时, 取消之前的发送
testCancel.value && testCancel.value.cancel()
// 生成新的 source
const source = CancelToken.source()
// 重新赋值
testCancel.value = source
// 发送请求
getNewList({cancelToken: source.token})
} catch (error) {
}
}
// 方式 2
const testCancel = ref<Canceler | null>(null)
const sendPost = async () => {
try {
// 重复操作时, 取消之前的发送
testCancel.value && testCancel.value()
// 重新赋值
const res = await getNewList({cancelToken: new CancelToken(c => testCancel.value = c)})
console.log(res);
} catch (error) {
}
}
// 方式 3 (版本 大于等于 0.22.0)
const testCancel = ref<AbortController | null>(null)
const sendPost = async () => {
// newListStore.getNewList(100, newListStore.states)
try {
// 重复操作时, 取消之前的发送
testCancel.value && testCancel.value.abort()
// 生成新的 controlLer
const controlLer = new AbortController()
// 重新赋值
testCancel.value = controlLer
const res = await getNewList({signal: controlLer.signal})
console.log(res);
} catch (error) {
}
}
</script>
<template>
<input type="text" @input="sendPost">
</template>
<style scoed lang="less"></style>
api.js
/**
* options axios 额外的配置
* @param options
*/
export const getNewList = (options: any) => {
return axios({
method: 'GET',
url: 'http://geek.itheima.net/v1_0/articles',
params: {
channel_id: 100,
timestamp: Date.now()
},
...options
})
}