【问题标题】:Using only one reducer throw me errors with ngrx只使用一个减速器会在 ngrx 上引发错误
【发布时间】:2019-08-06 18:43:52
【问题描述】:

我正在使用商店开发 Angular,但由于一些 redux 错误,我无法构建我的项目。

实际上,让我的商店正常运行的唯一方法就是使用此配置

app.module.ts - 导入

StoreModule.forRoot({applicationState: AppReducer}),

app.reducer.ts

export function AppReducer(state = initialState, action: Action): AppState {
switch (action.type) {
    case types....:
      return {...state, ...};
...

在我的组件中

this.store.select(s => s.applicationState.myValue).subscribe()...

它没有正确构建,但是当我服务两次时,我至少可以测试我的项目。因为我没有任何子状态(我的 Appstate 充满了数字、数组等但没有子状态),并且只有一个减速器,所以我不知道应该如何在我的 app.module.ts 中声明我的 ActionMapReducer,以便能够订阅我的价值观是这样的:

this.store.select('myValue').subscribe(..)

我尝试了很多东西,查看了很多示例,但我不知道如何解决这个问题,因此我的项目无法构建:(。

【问题讨论】:

  • 你的减速器中的开关有默认值吗?
  • 是的,我愿意。其他一切都“正常”,我只是无法正确获取我的商店值。
  • 能否发送错误信息?

标签: angular dictionary redux store ngrx


【解决方案1】:

看来你不能在没有ReducerMap 的情况下使用ngrx 的商店,即使你只有一个reducer。 :(

这样就可以了

index.ts

export interface State {
    state: AppState; // contains my store, real 'state'
}

export const reducers: ActionReducerMap<State> = { // State isnt my 'real' state !
  state: AppReducer
};

export const getValueState = createFeatureSelector<AppState>('state')
export const getValue = createSelector(getValueState, (state: AppState) => state.value);

app.moduls.ts - 导入

StoreModule.forRoot(reducers)

组件.ts

this.store.select(getValue).subscribe(...);

我仍然无法弄清楚为什么我不能使用像 select('myValue') 这样的选择器......但现在很好。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-04-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-28
    相关资源
    最近更新 更多