Angular / Redux / Reducers不导入

时间:2017-05-03 09:54:34

标签: angularjs redux

我的组合式减速器似乎有一个可能的注射问题。 Angular是创建Redux存储然后将此实例传递给React。我为此目的使用 ngReact ngRedux 库。

请原谅任何错别字。应用程序通过Webpack正确编译并运行,但这里详细说明了这个问题。

下面是我简单的Reducer

const botsReducerTemp = (state = [] , action) => {  
    switch(action.type) {
      case 'ADD_BOT_API':

        return state

      default:
        return state;
  }
}
export default botsReducerTemp;

我在一个名为 reducers 的文件夹中有一个 index.js ,它结合了这个reducer(以及其他几个但未在此问题中显示)

   import {combineReducers} from 'redux';
    import botsReducerTemp from './bots/bot-reducersTemp';

    const rootReducer =  combineReducers ({
        botsReducerTemp
    });

    export default rootReducer; 

在我的angular 1.5应用程序中,我有一个 module.js (导入的路径是正确的,并通过WebPack编译)

注意:导入import rootReducer from '../../reducers'这是导入 index.js

的组合缩减器
import ngreact from 'ngreact';
import ngRedux from 'ng-redux';
import thunk from 'redux-thunk';
import logger from 'redux-logger'
import rootReducer from '../../reducers'

angular
  .module('meso-app', [
    'meso-core',
    'ngFileUpload',
    'chart.js',
    'ngRedux'
  ])

 .config(($ngReduxProvider) => {
      $ngReduxProvider.createStoreWith(rootReducer, [thunk], [logger], [{ tests: {id: 1, isDone: false, text: 'bot 1'}}] );
      console.log('rootReducer = ' + typeof(rootReducer));
      console.dir(rootReducer);
  })

......lot's other angular boot stuff....

问题在我的 .config rootReducer 在Chrome调试中查看时显示为不可用。< / p>

$ ngReduxProvider.createStoreWith 执行正常,但由于rootReducer为空,因此不会构建Redux存储。

rootReducer 是在 .createStoreWith

中执行/解析的函数

那么如何注入/导入这个功能呢?

2 个答案:

答案 0 :(得分:0)

module.js 中删除了导入并将其替换为require

var rootReducer = require('../../reducers');

然后在我的.config中使用了

 let rootReducers = rootReducer.rootReducer;

然后调用createStoreWith,如下所示

  $ngReduxProvider.createStoreWith(rootReducers, [thunk], null, [{ tests: {id: 1, isDone: false, text: 'bot 1'}}] );

答案 1 :(得分:0)

我正在遵循类似的模式,我能够通过以下方式实现它:

import { combineReducers } from 'redux';
import { ExampleReducer } from './example';

export const RootReducer = combineReducers({
    example: ExampleReducer
});

然后在你的.config:

...
import { RootReducer } from '../../reducers';
...
.config(($ngReduxProvider) => {
  $ngReduxProvider.createStoreWith(RootReducer, [thunk], [logger], [{ tests: {id: 1, isDone: false, text: 'bot 1'}}] );
  console.log('rootReducer = ' + typeof(RootReducer));
  console.dir(RootReducer);
})