我目前正在将React实现到我的中,并且我在获取数据方面遇到了问题。
我想要创建一个端点,查询需要多个参数才能工作。例如,端点是https://localhost:8080/object/id,其中对象和Id是我需要从其中获取数据的参数。
我为查询创建了如下接口:
export interface ISearch {
objectName: string,
id: string
}我的apiSlice看起来是这样的:
import {createApi, fetchBaseQuery} from "@reduxjs/toolkit/query";
import {ISearch} from "../utils/Interfaces";
export const apiSlice = createApi({
reducerPath: 'api',
baseQuery: fetchBaseQuery({
baseUrl: 'https://localhost:8080/api/v1'
}),
tagTypes: ['Record'],
endpoints: builder => ({
getRecord: builder.query({
query: (params: ISearch) => `/${params.objectName}/${params.id}`,
providesTags: (result, error, arg) => [{
type: 'Record',
id: arg
}]
})
})
})唯一的问题是,我在providesTags函数上得到了以下错误:
TS2322: Type '(result: any, error: FetchBaseQueryError, arg: ISearch) => { type: "Record"; id: ISearch; }[]' is not assignable to type 'ResultDescription<"ObjectDefinition" | "Record", any, ISearch, FetchBaseQueryError, FetchBaseQueryMeta>'. Type '(result: any, error: FetchBaseQueryError, arg: ISearch) => { type: "Record"; id: ISearch; }[]' is not assignable to type 'GetResultDescriptionFn<"ObjectDefinition" | "Record", any, ISearch, FetchBaseQueryError, FetchBaseQueryMeta>'. Type '{ type: "Record"; id: ISearch; }[]' is not assignable to type 'readonly TagDescription<"ObjectDefinition" | "Record">[]'. Type '{ type: "Record"; id: ISearch; }' is not assignable to type 'TagDescription<"ObjectDefinition" | "Record">'. Type '{ type: "Record"; id: ISearch; }' is not assignable to type 'FullTagDescription<"ObjectDefinition" | "Record">'. Types of property 'id' are incompatible. Type 'ISearch' is not assignable to type 'string | number'. Type 'ISearch' is not assignable to type 'number'.发布于 2022-03-12 22:21:19
查询标记由类型和字符串id组成。arg在您的providesTags函数中是您在query函数中所称的params。因此,与其在那里使用arg作为id,不如使用arg.id或像arg.object + '/' + arg.id这样的连接字符串。
https://stackoverflow.com/questions/71451175
复制相似问题