redux-toolkit prepareHeaders 不是函数
redux-toolkit prepareHeaders is not a function
我在使用此查询时遇到问题,它说 prepareHeaders
不是函数,但我将其添加为对象,如下面的代码所示:
const access_token = localStorage.getItem('rs_access_token');
// Define a service using a base URL and expected endpoints
export const lfApi = createApi({
reducerPath: 'lfApi',
baseQuery: fetchBaseQuery({
baseUrl: 'http://homestead.test/api/v1',
prepareHeaders: {
'Authorization': `Bearer ${access_token}`,
'Content-Type': 'application/json',
'Accept': 'application/json',
},
}),
endpoints: (builder) => ({
getUser: builder.query({
query: (params) => `user`,
}),
getAgent: builder.query({
query: (params) => `myagents/agents/${params}`,
}),
}),
});
// Export hooks for usage in functional components, which are
// auto-generated based on the defined endpoints
export const { useGetUserQuery } = lfApi;
export const { useGetAgentQuery } = lfApi;
这是因为 prepareHeaders
属性 - 正如错误消息所说 - 接受一个函数,而不是一个对象。从例子中可以看出here.
const baseQuery = fetchBaseQuery({
baseUrl: '/',
prepareHeaders: (headers, { getState }) => {
headers.set('Authorization', `Bearer ${access_token}`);
headers.set('Content-Type', 'application/json');
headers.set('Accept', 'application/json');
return headers
},
})
我在使用此查询时遇到问题,它说 prepareHeaders
不是函数,但我将其添加为对象,如下面的代码所示:
const access_token = localStorage.getItem('rs_access_token');
// Define a service using a base URL and expected endpoints
export const lfApi = createApi({
reducerPath: 'lfApi',
baseQuery: fetchBaseQuery({
baseUrl: 'http://homestead.test/api/v1',
prepareHeaders: {
'Authorization': `Bearer ${access_token}`,
'Content-Type': 'application/json',
'Accept': 'application/json',
},
}),
endpoints: (builder) => ({
getUser: builder.query({
query: (params) => `user`,
}),
getAgent: builder.query({
query: (params) => `myagents/agents/${params}`,
}),
}),
});
// Export hooks for usage in functional components, which are
// auto-generated based on the defined endpoints
export const { useGetUserQuery } = lfApi;
export const { useGetAgentQuery } = lfApi;
这是因为 prepareHeaders
属性 - 正如错误消息所说 - 接受一个函数,而不是一个对象。从例子中可以看出here.
const baseQuery = fetchBaseQuery({
baseUrl: '/',
prepareHeaders: (headers, { getState }) => {
headers.set('Authorization', `Bearer ${access_token}`);
headers.set('Content-Type', 'application/json');
headers.set('Accept', 'application/json');
return headers
},
})