如何将参数添加到 RTK Redux Toolkit 中的 createAPI?
How do I add params to a createAPI in RTK Redux Toolkit?
这就是我的 createAsyncThunk 的样子
export const fetchMapData = createAsyncThunk(
'mapdata/fetchMapData',
async (id) => {
const response = await axios.get(
GLOBALVIEWS_API,
{
headers: {
'Content-Type': 'application/json',
'X-API-KEY': HBO_KEY,
},
params: {
titleId: id,
}
}
)
return response.data.Item;
}
)
我需要把它变成一个 createApi() 但下面这个在参数要求上仍然失败
export const apiSlice = createApi({
reducerPath: 'api',
baseQuery: fetchBaseQuery({
baseUrl: BASE_URL,
prepareHeaders: (headers, { getState }) => {
headers.set('Content-Type', 'application/json')
headers.set('X-API-KEY', HBO_KEY)
return headers
}
}),
endpoints:(builder) => ({
getMedia: builder.query({
query:(id) => {
console.log("titleId: ", id);
return{
url: '/titlestest',
params: {titleId: id}
}
}
})
})
})
export const apiSlice = createApi({
reducerPath: 'api',
baseQuery: fetchBaseQuery({
baseUrl: BASE_URL,
prepareHeaders: (headers, { getState }) => {
headers.set('Content-Type', 'application/json')
headers.set('X-API-KEY', HBO_KEY)
return headers
}
}),
endpoints:(builder) => ({
getMedia: builder.query({
query: (id) => `/titletest/${id}`,
// or
query: (id) => ({ url: `/titletest/${id}` }),
// or
query: ({ title, id }) => `/${title}/${id}`,
})
})
})
这就是我的 createAsyncThunk 的样子
export const fetchMapData = createAsyncThunk(
'mapdata/fetchMapData',
async (id) => {
const response = await axios.get(
GLOBALVIEWS_API,
{
headers: {
'Content-Type': 'application/json',
'X-API-KEY': HBO_KEY,
},
params: {
titleId: id,
}
}
)
return response.data.Item;
}
)
我需要把它变成一个 createApi() 但下面这个在参数要求上仍然失败
export const apiSlice = createApi({
reducerPath: 'api',
baseQuery: fetchBaseQuery({
baseUrl: BASE_URL,
prepareHeaders: (headers, { getState }) => {
headers.set('Content-Type', 'application/json')
headers.set('X-API-KEY', HBO_KEY)
return headers
}
}),
endpoints:(builder) => ({
getMedia: builder.query({
query:(id) => {
console.log("titleId: ", id);
return{
url: '/titlestest',
params: {titleId: id}
}
}
})
})
})
export const apiSlice = createApi({
reducerPath: 'api',
baseQuery: fetchBaseQuery({
baseUrl: BASE_URL,
prepareHeaders: (headers, { getState }) => {
headers.set('Content-Type', 'application/json')
headers.set('X-API-KEY', HBO_KEY)
return headers
}
}),
endpoints:(builder) => ({
getMedia: builder.query({
query: (id) => `/titletest/${id}`,
// or
query: (id) => ({ url: `/titletest/${id}` }),
// or
query: ({ title, id }) => `/${title}/${id}`,
})
})
})