const instance = axios.create({ baseURL: 'https://v2.com/', timeout: 20000, headers: { 'Content-Type': 'application/json', 'Accept': 'application/json' } });export const post = (url, data) => { return instance .post(url, data) .catch(handleError); }function handleError(error) { toast.toastDanger(error.message || 'request error'); console.error(error) Promise.reject(error) }
调用:
api.post('jwt-token-auth/', body) .then(function (response) { vm.loading = false; console.log(response) vm.$router.replace({path: '/home'}) })
想在全局catch异常,可是因为提前catch了,后面调用时就算发生错误也会进入then代码块,请问如何正确封装axios类似的http请求库?
紫衣仙女
相关分类