【bug记录】 Argument of type ‘AsyncThunkAction<void, string, {}>‘ is not assignable to parameter of type
const dispatch = useDispatch(); // 这里const dispatch: Dispatch<AnyAction>useEffect(() => {dispatch(getProductDetail(touristRouteId));}, [])
getProductDetail的类型是 AsyncThunk<void, string, {}>,但是const dispatch: Dispatch<AnyAction>。
export const getProductDetail=createAsyncThunk("productDetail/getProductDetail",async (touristRouteId:string, thunkAPI) => {const fetchData = async () => {thunkAPI.dispatch(productDetailSlice.actions.fetchStart());try {const { data } = await axios.get(`http://123.56.149.216:8080/api/touristRoutes/${touristRouteId}`);thunkAPI.dispatch(productDetailSlice.actions.fetchSuccess(data));} catch (error) {thunkAPI.dispatch(productDetailSlice.actions.fetchFail(error.message));}};fetchData();}
)
两种解决办法:
一是定义getProductDetail是any类型
export const getProductDetail:any=createAsyncThunk("productDetail/getProductDetail",async (touristRouteId:string, thunkAPI) => {const fetchData = async () => {thunkAPI.dispatch(productDetailSlice.actions.fetchStart());try {const { data } = await axios.get(`http://123.56.149.216:8080/api/touristRoutes/${touristRouteId}`);thunkAPI.dispatch(productDetailSlice.actions.fetchSuccess(data));} catch (error) {thunkAPI.dispatch(productDetailSlice.actions.fetchFail(error.message));}};fetchData();}
)
二是用getDefaultMiddleware().concat而不是[...getDefaultMiddleware(),...]
const store = configureStore ({reducer:rootReducer,// 不要用展开运算符这种写法 middleware: (getDefaultMiddleware) => [...getDefaultMiddleware(), actionLog, changeLanguage],middleware: (getDefaultMiddleware) => getDefaultMiddleware().concat(actionLog,changeLanguage), devTools:true
});
【bug记录】 Argument of type ‘AsyncThunkAction<void, string, {}>‘ is not assignable to parameter of type
const dispatch = useDispatch(); // 这里const dispatch: Dispatch<AnyAction>useEffect(() => {dispatch(getProductDetail(touristRouteId));}, [])
getProductDetail的类型是 AsyncThunk<void, string, {}>,但是const dispatch: Dispatch<AnyAction>。
export const getProductDetail=createAsyncThunk("productDetail/getProductDetail",async (touristRouteId:string, thunkAPI) => {const fetchData = async () => {thunkAPI.dispatch(productDetailSlice.actions.fetchStart());try {const { data } = await axios.get(`http://123.56.149.216:8080/api/touristRoutes/${touristRouteId}`);thunkAPI.dispatch(productDetailSlice.actions.fetchSuccess(data));} catch (error) {thunkAPI.dispatch(productDetailSlice.actions.fetchFail(error.message));}};fetchData();}
)
两种解决办法:
一是定义getProductDetail是any类型
export const getProductDetail:any=createAsyncThunk("productDetail/getProductDetail",async (touristRouteId:string, thunkAPI) => {const fetchData = async () => {thunkAPI.dispatch(productDetailSlice.actions.fetchStart());try {const { data } = await axios.get(`http://123.56.149.216:8080/api/touristRoutes/${touristRouteId}`);thunkAPI.dispatch(productDetailSlice.actions.fetchSuccess(data));} catch (error) {thunkAPI.dispatch(productDetailSlice.actions.fetchFail(error.message));}};fetchData();}
)
二是用getDefaultMiddleware().concat而不是[...getDefaultMiddleware(),...]
const store = configureStore ({reducer:rootReducer,// 不要用展开运算符这种写法 middleware: (getDefaultMiddleware) => [...getDefaultMiddleware(), actionLog, changeLanguage],middleware: (getDefaultMiddleware) => getDefaultMiddleware().concat(actionLog,changeLanguage), devTools:true
});