这是我的服务,我在其中调用api(带参数(
const battle = async (obj:any): Promise<result[]> =>{
const data=await fetch(`${API_URL}`,{
method: "POST",
// Adding body or contents to send
body: JSON.stringify({
"1stArg": obj.arg1,
"2ndArg": obj.arg2
}),
// Adding headers to the request
headers: {
"Content-type": "application/json; charset=UTF-8"
}
});
const res = await data.json()
return res;
}
这是我的行动,我打电话给服务
export const getResult=createAsyncThunk<result[]>(
'testing/getResult',
async(obj:any)=>{
const data= await services.battle(obj);
return data;
}
);
这是还原剂
builder.addCase(getResult.pending, (state) => ({
...state,
result: null,
}));
builder.addCase(getResult.rejected, (state) => ({
...state,
result: null,
}));
builder.addCase(getResult.fulfilled, (state, action) => ({
...state,
result: action.payload,
}));
选择器
export const getFinalResult = (state: RootState) => state.monsters.result;
这就是我使用的方式
const result=useSelector(getFinalResult);
const handleStartBattleClick = () => {
let obj={
fistArg,2ndArg
}
dispatch(getResult(obj))
}
并且得到这个错误
需要0个参数,但得到1个
如果您在上面指定了泛型,则必须完全这样做:
export const getResult=createAsyncThunk<ResultType, ArgumentType>(
否则它将回落到void
;没有争论";。