在书写请求的文件里面书写如下代码
import Axios from 'axios'
const CancelToken = Axios.CancelToken
export let cancel = null
export const setCancel = (e) => {
cancel = e
}
在要取消的请求函数中添加如下代码
cancelToken: new CancelToken(function executor(c) {
// An executor function receives a cancel function as a parameter
cancel = c
})
例如:
export const Getlesson = (params) => {
const result = axios.get(`/odata/Article/GetDetailListByTag(tags='${params.TagChildrenId}')`, {
params: {
$top: params.Limit,
$skip: (params.Page - 1) * params.Limit,
$count: true,
$orderby: params.orderby, //ReadNum desc or CreateTime desc
$filter: `${ARTICLE_STATUS}`
},
cancelToken: new CancelToken(function executor(c) {
cancel = c
})
})
return result
}
然后在页面中请求发送的地方后面调用
setCancel函数将cancel置为空即可