代码之家  ›  专栏  ›  技术社区  ›  Sabbiu Shah

如何异步添加史诗?

  •  0
  • Sabbiu Shah  · 技术社区  · 6 年前

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

    跟踪此链接 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 = {};
    

    在这里,我用参数调用injectRepics(reducer和epics的Repics) key , reducer , newEpic 当新组件异步绑定时

      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;
    };
    
    1 回复  |  直到 6 年前
        1
  •  0
  •   Sabbiu Shah    6 年前

    有一个简单的错误。

    我忘了在新的史诗中加入组合词。

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

    推荐文章