【发布时间】:2019-01-10 11:01:19
【问题描述】:
我正在尝试研究如何在使用 axios 时处理错误。请看下面的代码:
//Admin.vue
methods: {
saveClicked: function(){
updateConfig(this.editedConfig)
.then(response => {
console.log("in here");
console.log(response);
this.info.showAlert = true;
this.info.message = "Successfully updated config!!"
this.info.alertType = "success";
this.showJsonEditor = !this.showJsonEditor;
})
.catch(error => {
this.info.showAlert = true;
this.info.message = "Failed to update config, please try again later"
this.info.alertType = "error";
});
}
}
//network.js
function updateConfig(editedConfig){
return axios.put(URL,editedConfig, {
headers: {
"Content-type": "application/json"
}
})
.then(response => response)
.catch(error => error);
}
请求成功后,一切正常。我收到警报说一切都很好。
我强迫我的后端为每个请求返回一个错误,这样我就可以在我的 Vue 应用程序中模拟错误行为,这就是我观察到的:
即使收到错误消息。该程序正在通过then()Admin.vue查看下图:
我错过了什么?
【问题讨论】:
标签: vue.js error-handling vuejs2 axios