【发布时间】:2021-03-05 03:52:03
【问题描述】:
我计划通过 axios 通过 vuex 执行所有服务器 CRUDing 传输。喜欢:
actions: { // dispatch
createCategory(ctx) {
return axios.post(process.env.API_URL + "category/create/", category)
.then(response => {
ctx.commit('UPDATE_CATEGORY', response.data.updated_category);
return response;
}).catch(error => {
return error;
})
...
然后在像这样的组件中调用它们
this.$store.dispatch("createCategory", category)
.then(response => { DO NOTIFICATION STUFF }
.catch(error => { this.errors = error }
(我可能需要 axios 中的 promise 和 reject() 来捕获错误,但这不是重点)
我的问题是我应该在本地以这种方式(组件)处理错误,还是应该使用类似 vuex 的状态全局处理它
actions: { // dispatch
createCategory(ctx) {
return axios.post(process.env.API_URL + "category/create/", category)
.then(response => {
ctx.commit('UPDATE_CATEGORY', response.data.updated_category);
return response;
}).catch(error => {
ctx.commit('SET_ERROR', error)
})
...
并在组件中调用错误,例如:this.$store.getters['getErrors']?
(这里的例子有点抽象,只是为了解释)
那么,在组件内本地处理错误还是在 vuex 商店内全局处理错误,哪种方式更好?
【问题讨论】:
标签: javascript vue.js axios vuex