【发布时间】:2018-03-13 23:18:56
【问题描述】:
这很好,可能会被标记为重复,如果是我很抱歉。一直在谷歌上搜索很多以找到如何实际执行此操作,但没有任何适当的解决方案(虽然我不是 vue 专家..)
基本上..我正在尝试做什么..从 api => store => vue 组件传递成功或失败。如果错误...我将向用户显示错误代码(现在...)
事情的方式..
1) 从 vue 组件触发的方法。发送到 $store (modal.vue)
2) 触发状态动作来设置突变类型和调用API。
3) Api 方法被调用。
4) 返回成功或错误,以及 http.statuscode....
MODAL.VUE
doRefund: function(){
this.$store.dispatch('doRefund', {
Username : this.loggedInUser.account.username,
OrderID: this.selectedOrder.orderid,
IsFeeApplied: false,
CreditAmount: this.refundAmount,
ChargeFee: 0.0,
Reason: "reason-not-specified",
Description: this.comment,
BearerToken: "Bearer " + this.accessToken
})
.then(result => {
if(result === true){
alertify.success('It worked!')
}
else{
alertify.alert('There was an error, and the errorcode is' + errorcode ????)
}
})
}
STORE.JS
doRefund({ commit }, refundParams){
api.tryRefund(refundParams)
.then(refundCompleted => {
commit(types.SET_REFUND_COMPLETED, true)
return true;
})
.catch(err => {
//TODO: How to i fetch, and pass the errorcode ?
commit(types.SET_REFUND_COMPLETED, false)
return false;
})
},
API.JS
tryRefund(refundParams) {
console.log('=== try ====');
console.log( refundParams );
return new Promise((resolve, reject) => {
var config = {
headers: {
'Content-Type': ' application/json',
'Authorization': refundParams.BearerToken
}
};
return axios.post('the-url-to-the-service', refundParams, config)
.then(
() => resolve(true))
.catch( error => {
console.log('=== ERROR ====');
console.log( error.response );
})
});
}
【问题讨论】: