下面的代码有什么问题?我得到 async is a reserved word 错误,我为 async/await 配置了 babel。
export async function getCredit(){
return dispatch => {
try {
const creditInfo = await axios.get(`/credit`)
} catch (err) {
dispatch(errorMsg(err.data.msg))
}
if(creditInfo.result === 200 && res.data.status === 1) {
dispatch({
type: GET_CREDIT,
payload: creditInfo.data
})
}
}
}
我什至尝试过这个
export async getCredit() => dispatch => {
try {
const creditInfo = await axios.get(`/credit`)
} catch (err) {
dispatch(errorMsg(err.data.msg))
}
if(creditInfo.result === 200 && res.data.status === 1) {
dispatch({
type: GET_CREDIT,
payload: creditInfo.data
})
}
}
最佳答案
这是一个高阶函数(返回一个函数的函数),你应该将 async
添加到使用 await
的嵌套函数中,而不是外部函数(它只返回一个函数)。
export function getCredit(){
return async dispatch => {
关于reactjs - 使用 async await 和 redux reducer,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50055958/