如何异步添加epics?

How to add epics asynchronously?

我可以添加异步减速器,但无法添加异步 epics

在此 link Adding New Epics Asynchronously 之后,我尝试使用 epic$.next() 但无法添加新的史诗。

import { applyMiddleware, createStore, compose } from 'redux';
import { createEpicMiddleware, combineEpics } from 'redux-observable';
import { BehaviorSubject } from 'rxjs';
import { mergeMap } from 'rxjs/operators';
import createReducer from '../reducers';
import mainEpic from '../config/epics';

中间件配置如下:

const epicMiddleware = createEpicMiddleware();

const epic$ = new BehaviorSubject(mainEpic);
const rootEpic = (action$, state$) =>
  epic$.pipe(mergeMap(epic => epic(action$, state$)));

redux 开发工具的商店增强器

const composeEnhancers =
  // compose;
  process.env.NODE_ENV === 'development' &&
  window.navigator.platform !== 'iPad' &&
  window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__
    ? window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__
    : compose;

这是store的实例化

export default () => {
  const store = createStore(
    createReducer(),
    composeEnhancers(applyMiddleware(epicMiddleware))
  );

  epicMiddleware.run(rootEpic);

  // Extra functionality to the store
  store.asyncReducers = {};

在这里,我使用参数 keyreducernewEpic 调用 injectRepics(Repics 用于 reducer 和 epics)异步绑定新组件时

  store.injectRepics = (key, reducer, newEpic) => {
    if (!store.getState()[key]) {

      // here I get newEpic
      console.log(newEpic);

      // new reducer is added to asyncreducer object
      store.asyncReducers[key] = reducer;
      // new reducer is created and replaced
      store.replaceReducer(createReducer(store.asyncReducers));

      // -------------------------------------
            But, I'm unable to replace epic
      // -------------------------------------
      newEpic && epic$.next(newEpic);

      // epicMiddleware.run(rootEpic);
      // newEpic && addNewEpic(newEpic);
    }
  };

  return store;
};

有一个简单的错误。

我忘记在新史诗中添加 combineEpics。

所以,代码变成newEpic && epic$.next(combineEpics(...newEpic))