代码之家  ›  专栏  ›  技术社区  ›  kevin

Redux RTK查询:getDefaultMiddleware不是函数

  •  2
  • kevin  · 技术社区  · 4 年前

    在哪里 getDefaultMiddleware 来自哪里?我在看报纸 docs 它似乎神奇地出现在配置存储中。虽然这很好,但它并没有进入我的商店,而且。。。因为这个函数没有导入路径,我也不知道如何处理这个问题,所以我可以使用一些指导。

    rtk版本: "@reduxjs/toolkit": "^1.6.1",

    太长,读不下去了

    getDefaultMiddleware() 在我的商店中不存在+我还没有在文档中找到任何关于它的有用信息。

    以下是我得到的错误:

    enter image description here

    这是我的 store.ts

    import { Action, configureStore, ThunkAction } from '@reduxjs/toolkit';
    import { setupListeners } from '@reduxjs/toolkit/query';
    import { myApi } from './services/api';
    
    import authorReducer from './slices/authorSlice';
    import transcriptReducer from './slices/transcriptSlice';
    
    export const store = configureStore({
      reducer: {
        [myApi.reducerPath]: myApi.reducer,
        middleware: (getDefaultMiddleware) =>
          // "This expression is not callable."
          getDefaultMiddleware().concat(myApi.middleware),
        author: authorReducer,
        transcript: transcriptReducer,
      },
    });
    
    setupListeners(store.dispatch);
    
    // Infer the `RootState` and `AppDispatch` types from the store itself
    export type RootState = ReturnType<typeof store.getState>;
    export type AppDispatch = typeof store.dispatch;
    export type AppThunk<ReturnType = void> = ThunkAction<
      ReturnType,
      RootState,
      unknown,
      Action<string>
    >;
    

    这是我的 api.ts

    import { createApi, fetchBaseQuery } from '@reduxjs/toolkit/query/react';
    
    const apiEndpoint ='http://localhost:5000';
    
    export const myApi = createApi({
      reducerPath: 'myApi',
      baseQuery: fetchBaseQuery({ baseUrl: apiEndpoint }),
      endpoints: builder => ({
        getFileById: builder.query<any, { id: string; fileId: string }>({
          query: arg => {
            const { id, fileId } = arg;
            return `/some/endpoint/with/two/${id}/pathvariables/${fileId}`;
          },
        }),
      }),
    });
    
    // RTK Query will automatically generate hooks for each endpoint query
    export const { useGetFileByIdQuery } = myApi;
    
    0 回复  |  直到 4 年前
        1
  •  2
  •   kevin    4 年前

    答复:

    我定义了中间件 在…内 reducer: { ... } 1.我得把它搬走 外部 那个减速机的。问题解决了。

    export const store = configureStore({
      reducer: {
        [myApi.reducerPath]: myApi.reducer,
        author: authorReducer,
        transcript: transcriptReducer,
      },
      middleware: getDefaultMiddleware =>
        getDefaultMiddleware().concat(myApi.middleware),
    });
    
    推荐文章