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

Redux中无法使用applyMiddleWare的问题求助

解决Redux中applyMiddleware导入失败的问题

嘿,我一眼就发现问题所在啦!你遇到的导入错误完全是因为拼写错误——Redux里这个中间件工具的正确拼写是applyMiddleware,而你写成了applyMiddleWare(注意最后一个W不该大写哦)。

正确的导入方式

Redux 4.0.0版本中,applyMiddleware是具名导出,修正拼写后,用原来的具名导入方式即可正常使用:

import { createStore, combineReducers, applyMiddleware } from "redux";

完整用法示例(以redux-logger为例)

如果要结合redux-logger使用中间件,参考下面的完整代码:

import { createStore, combineReducers, applyMiddleware } from "redux";
import logger from "redux-logger";

// 合并你的reducers
const rootReducer = combineReducers({
  // 这里填入你的各个reducer,比如:
  // counter: counterReducer
});

// 创建store并应用中间件
const store = createStore(
  rootReducer,
  applyMiddleware(logger)
);

为什么之前的导入会失败?

你之前的两种尝试都因为拼写错误导致Redux找不到对应的导出:

  • 第一种具名导入时,Redux里根本没有applyMiddleWare这个导出,所以报错'redux' does not contain an export named 'applyMiddleWare'
  • 第二种默认导入时,Redux并没有将applyMiddleware设为默认导出,自然找不到对应内容,所以报错"export 'default' (imported as 'applyMiddleWare') was not found in 'redux'

修正拼写后就能正常使用啦!

内容的提问来源于stack exchange,提问作者Thakur Karthik

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.29 08:36:05