基本上,我的问题是https://github.com/axios/axios/issues/583中的确切问题。可以通过throw new axios.Cancel('Operation canceled by the user.');
来完成。但是如何在nuxt axios模块中做到这一点呢?我在文档中看不到它,尝试了$axios.Cancel('Error')
,但是返回了$ axios.Cancel不是构造函数
基本上,下面的代码片段就是我要寻找的内容:
axios.interceptors.response.use(function (response) {
throw new axios.Cancel('Operation canceled by the user.');
}, function (error) {
return Promise.reject(error);
});
强调throw new axios.Cancel
答案 0 :(得分:1)
尽管@nuxtjs/axios
没有公开axios.Cancel
,但是您仍然可以直接导入axios
来获得该符号。注意axios
已经是@nuxtjs/axios
的依赖项,因此不需要额外的依赖项。
示例(已通过@nuxtjs/axios
v5.11.0测试):
// plugins/axios.js
import { Cancel } from 'axios'
export default function ({ $axios }) {
$axios.onResponse((response) => {
if (response.code !== 200){
throw new Cancel(response.msg)
}
})
}
答案 1 :(得分:0)
答案 2 :(得分:0)
好像nuxt-axios并没有确切的等效物,但是我找到了解决方法。
plugins / axios.js
export default function({ $axios, req, store, redirect, app }, inject) {
// const source = $axios.CancelToken.source()
const timeout = process.env.API_TIMEOUT || 10000
const errorHandling = function(error) {
console.log(`API ${error}`)
return new Promise(() => {})
}
$axios.onResponse((response) => {
// Any condition that could be considered an response based on standard response
if(response.code !== 200){
throw response.msg
}
})