我的dispatch是这样的
dispatch(loginAction(params))
.then((action) => {
console.log(action)
message.success('登录成功')
navigate('/')
})
.catch((error) => {
message.error('error')
})
store的异步设这样写的
export const loginAction = createAsyncThunk("user/loginAction", async (payload: UserApi.LoginParam) => {
return await userApi.getToken(payload)
})
我先把后端服务关了,那么这个请求一定会超时报错
结果login页面中的dispatch().then().catch(),中并没有触发catch,而是触发了then(),这是打印的then的参数,也就是说即使失败了,diapatch也认为是成功的,只是把错误信息给带回来了,有点不能理解为啥是这样,直接触发catch多好,这样就不用去判断了
{
"type": "user/loginAction/rejected",
"meta": {
"arg": {
"username": "panda",
"password": "123456"
},
"requestId": "Jk8otrRUGQ9V4OPak93Nu",
"rejectedWithValue": false,
"requestStatus": "rejected",
"aborted": false,
"condition": false
},
"error": {
"name": "AxiosError",
"message": "Network Error",
"stack": "AxiosError: Network Error\n at XMLHttpRequest.handleError (http://localhost:8080/static/js/bundle.js:68015:14)",
"code": "ERR_NETWORK"
}
}