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請求庫?
vue2 中如何封裝http請求
米琪卡哇伊
2018-10-18 10:13:20