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)
}
)
}
}