我正在使用Redux的createAsyncThunk函数作为api请求。关闭网络时,以下函数返回action.payload=“网络错误”和状态=“成功”。状态应该是“错误”。但它回报了成功。有什么解决问题的建议吗?提前谢谢。
从“@reduxjs/toolkit”导入
{ createAsyncThunk,createSlice };从“axios”导入axios;从“./API”导入{ GET_POSTS_API };const initialState ={ posts:[],状态:“空闲”,错误: null };导出const getPosts = createAsyncThunk("posts/getPostsStatus“),异步c () => { try { const {数据}=等待axios.get(GET_POSTS_API);返回数据;} catch (错误){返回error.response?error.response.data.message : error.message;}};导出const postsSlice = createSlice({ name:"posts",initialState,减速器:{},extraReducers:{ getPosts.pending:(state,action) => { state.status = "loading";},getPosts.fulfilled:(state,action) => { state.status = "success";state.posts = action.payload;},getPosts.rejected:(状态,动作) => { state.status = "error";state.error = action.payload;},},});导出默认postsSlice.reducer;
发布于 2022-10-10 07:29:42
若要打印错误,请执行以下操作:
[getPosts.rejected]: (state, action) => {
console.log(action.error)
},https://stackoverflow.com/questions/68928867
复制相似问题