axios返回包装函数,但我想在其中使用 axios.CancelToken 。为什么会这样?
import axios from "axios";
const instance = axios.create({
baseURL: appUrl,
timeout: 0,
});
instance.interceptors.request.use(
function(config) {
console.log(instance);
}
);
export const $axios = instance;
答案 0 :(得分:2)
CancelToken
是axios
的静态属性。这不是实例属性。
如果您使用的是ES6模块导入,引用它的一种简单方法是导入
import axios, { CancelToken } from "axios"
const instance = axios.create({
baseURL: appUrl,
timeout: 0,
});
const source = CancelToken.source()
instance.post('/user/12345', {
name: 'new name'
}, {
cancelToken: source.token
})
// cancel the request (the message parameter is optional)
source.cancel("Operation cancelled by the user.");
这实际上只是
的短版import axios from "axios"
const CancelToken = axios.CancelToken