未捕获的类型错误:提供您的根 Epic 来 createEpicMiddleware(rootEpic)

2024-05-04

我收到这个错误

未捕获的类型错误:将您的根 Epic 提供给createEpicMiddleware(rootEpic)不再支持,而是使用epicMiddleware.run(rootEpic)

简单使用时

import 'rxjs'
import { createStore, combineReducers, applyMiddleware } from 'redux'
import { reducer as formReducer } from 'redux-form'
import thunk from 'redux-thunk'
import promise from 'redux-promise-middleware'
import { createEpicMiddleware, combineEpics } from 'redux-observable'

import app from './app'

// Bundling Epics
const rootEpic = combineEpics(
)

// Creating Bundled Epic
const epicMiddleware = createEpicMiddleware(rootEpic)

// Define Middleware
const middleware = [
  thunk,
  promise(),
  epicMiddleware
]

// Define Reducers
const reducers = combineReducers({
  form: formReducer
})

// Create Store
export default createStore(reducers,window.__REDUX_DEVTOOLS_EXTENSION__ && window.__REDUX_DEVTOOLS_EXTENSION__(), applyMiddleware(...middleware))

请帮助解决这个问题


首先,看一下这个文档:Redux-Observable 官方文档 https://redux-observable.js.org/docs/api/createEpicMiddleware.html因为我们使用的是最新版本的 Redux-Observable,所以查看它的文档非常有帮助。

查看完文档后,让我们看一个小示例项目(Counter 应用程序):

这是root.js捆绑后包含我的 Epics 和 Reducers 的文件。

// This is a sample reducers and epics for a Counter app.
import { combineEpics } from 'redux-observable';
import { combineReducers } from 'redux';


import counter, {
  setCounterEpic,
  incrementEpic,
  decrementEpic
} from './reducers/counter';

// bundling Epics
export const rootEpic = combineEpics(
  setCounterEpic,
  incrementEpic,
  decrementEpic
);

// bundling Reducers
export const rootReducer = combineReducers({
  counter
});

这是store.js我在使用它之前定义我的商店。

import { createStore, applyMiddleware } from 'redux';
import { createEpicMiddleware } from 'redux-observable';
import { rootEpic, rootReducer } from './root';
import { composeWithDevTools } from 'redux-devtools-extension';

const epicMiddleware = createEpicMiddleware();

const middlewares = [
  epicMiddleware
]

const store = createStore(
  rootReducer,
  composeWithDevTools(applyMiddleware(middlewares))
);

epicMiddleware.run(rootEpic);

export default store;

为了顺利实施redux-observable,我们必须遵守这个命令:

  1. 使用创建epicMiddlewarecreateEpicMiddleware() method
  2. 使用applyMiddleware()注册epicMiddleware的方法(redux-devtools-extension是可选的)
  3. 呼叫epicMiddleware.run()rootEpic我们之前创建的。

这是 Redux-Observable 文档中的指令

欲了解更多信息,您可以在这里找到它::设置中间件 https://redux-observable.js.org/docs/basics/SettingUpTheMiddleware.html:

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

未捕获的类型错误:提供您的根 Epic 来 createEpicMiddleware(rootEpic) 的相关文章

随机推荐