You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
batchAddVariable({globalParamList: validList}).then(res=>{if(res===SERVER_ERROR_CODE.SUCCESS){// !!!Bad: how many interface, how many judge 200this.close(true)this.$toast.show(res.detail.colletion)// !!!Bad: always get detail data}else{// !!!Bad: too much nest,reading difficultythis.$toast.show(res.desc)if(res===SERVER_ERROR_CODE.REPEAT){
...
}}})
对于接口层来说,后端经常定义的结构如下:
bad
good
解决方案
http层axios拿到数据后进行统一处理
到此基本就可以很优雅的写逻辑代码了。不过还有个点可以继续优化。通常情况,后台返回非200错误,只需要$toast提示结果就行,catch代码部分可以省略。类似如下:
多么简洁的代码,但Promise执行reject代码,浏览器会报
Uncaught (in promise)
错误。这是因为中断了Promise操作但又没有对其进行处理,故由此错误。只需要使用unhandledrejection
全局处理即可。The text was updated successfully, but these errors were encountered: