You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在RTK Query中配置多Base URL的API切片至Redux Store

解决RTK Query多API切片的Store配置警告

问题出在你只将webApiSlice的中间件添加到了Redux Store中,而apiSlice对应的中间件没有被加入,导致RTK Query无法为apiSlice提供缓存自动清理、自动重取等核心功能,从而触发警告。

正确的做法是将两个API切片的中间件都添加到Store的middleware数组中,修改后的store.js代码如下:

import { configureStore } from '@reduxjs/toolkit';
import authReducer from '../features/auth/authSlice';
import { webApiSlice } from './api/webApiSlice';
import { apiSlice } from './api/apiSlice';

const store = configureStore({
    reducer: {
        [webApiSlice.reducerPath]: webApiSlice.reducer,
        [apiSlice.reducerPath]: apiSlice.reducer,
        auth: authReducer,
        // ... 其他reducers
    },
    middleware: (getDefaultMiddleware) => 
        getDefaultMiddleware().concat(webApiSlice.middleware, apiSlice.middleware)
});

export default store;

额外说明

每个RTK Query的createApi实例都会生成独立的reducer和middleware,必须确保:

  • 每个切片的reducerPath保持唯一(你当前的api和webapi已经满足要求)
  • 所有切片的middleware都被添加到Store的middleware配置中

内容的提问来源于stack exchange,提问作者Amirhossein Rahmati

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.15 08:30:53