将ActionReducerMap与StoreModule.forFeature一起使用会产生构建错误

时间:2019-10-02 06:21:46

标签: javascript angular typescript ngrx angular8

我的棱角项目结构如下:

/(root or repo folder)
 |_ projects
    |_ mylib (this is the main library that will be exported from the repo)
    |_ sample-app (created to consume 'mylib ' project to test that things work fine when 'mylib' would be consumed in other projects)

要处理应用程序状态,我正在使用ngRx(我仅具有基本知识)。该项目使用Angular8.2.5,ngRx 8.3.0和RxJs 6.5.3进行设置。

在仓库上执行npm start时,sample-app项目被引导,mylib项目被延迟加载。

这是我初始化应用商店/状态的方式

sample-app/app.module.ts(在sample-app项目内部)

StoreModule.forRoot({}, {
      runtimeChecks: {
        strictStateImmutability: true,
        strictActionImmutability: true,
        strictStateSerializability: true,
        strictActionSerializability: true,
      },
    }),
!environment.production ? StoreDevtoolsModule.instrument({ name: 'My Sample App' }) : [],

mylib/mylib.module.ts(在mylib项目内部)

import { libReducer} from './shared/store/lib.store';
StoreModule.forFeature('libState', libReducer)

libReducer是从mylib/shared/store/lib.store.ts文件中导出的

export interface subFeatureOneState {
    // some properties defined here
}

export interface LibState {
  subFeatureOneReducer: subFeatureOneState;
}

export const libReducer: ActionReducerMap<LibState> = {
  subFeatureOneReducer,
};

此设置唯一遇到的问题是,当我尝试构建项目(使用ng build)时遇到错误。

错误提示

enter image description here

检查日志也没有太大帮助。

如果我将StoreModule.forFeature中的mylib.module.ts定义更改为以下内容,则构建问题将得到解决

StoreModule.forFeature('libState', subFeatureOneReducer)

但这不是我想要的,因为我打算将所有减速器都放在一个位置,并且在StoreModule.forFeature项目中仅引用mylib

我在网上找不到太多的文章来说明功能模块存储中ActionReducerMap的用法。我采用了下面提到的方法,但是并没有解决构建失败的问题: @ngrx/store combine multiple reducers from feature module

配置存储/归约器以进行初始化的方式有问题吗?如果能在此获得任何指针来解决构建错误问题,那就太好了。

1 个答案:

答案 0 :(得分:1)

这是我的代码,我认为您缺少ROOT_REDUCERS

for category in self.categoryList {
        sentence += "Category: \(category)\n"
        for items in self.productList {
            if category == items.category {
                grandTotal += Double(items.qty)! * Double(items.price)!
                let total: Double = Double(items.qty)! * Double(items.price)!
                let item = (items.item as NSString).utf8String
                let qty  = (items.qty as NSString).utf8String
                let price = (items.price as NSString).utf8String
                sentence += String(format: "%-10s%20s%15s%15.02f\n", item!, qty!, price!, total)
            }
        }
        sentence += "\n\n"
        grandTotal = 0
    }

    lblData.text = sentence

这是您reference的完整代码