来自ngrx / store的减速机不会点火

时间:2017-09-25 16:10:23

标签: angular typescript redux ngrx-store

我在combineReducers"@angular/core": "4.4.3"项目中使用"@ngrx/store": "4.0.3",并且我在调度操作后遇到的问题没有得到解决。

这可能只是我对ngrx/store

的有限经验

整个项目可以在https://github.com/raduchiriac/workflow

看到

app.module.ts

import { AppStore } from './app.store'

@NgModule({
  imports: [
    StoreModule.forRoot(AppStore),
    ...
  ],
  providers: [
    ...
  ]
})

app.store.ts

import { createSelector } from 'reselect';
import * as ModalsReducer from "./store/reducers/modals.reducer";

export interface AppState {
  modals: ModalsReducer.State,
}

const metaReducer: ActionReducer<AppState> = combineReducers({
  modals: ModalsReducer.reducer,
});

export function AppStore(state: any, action: any) {
  return metaReducer(state, action);
}

modals.reducer.ts

import * as ModalsActions from '../actions/modals.actions';

export interface State {
  triggerAdd: boolean;
}

const initialState: State = {
  triggerAdd: false,
};

export function reducer(state = initialState, { type, payload }): State {
  switch (type) {
    case ModalsActions.MODALS_TRIGGER_ADD_CLOSE : {
      return Object.assign({...state}, {
        triggerAdd: false
      });
    }
    ...
}

triggers.component.ts

addNew() {
  this.store.dispatch(new ModalsActions.OpenTriggerAddAction());
}
...

编辑:商店运作的唯一方法就是这样做。为什么呢?

app.module.ts

import { AppStore, reducers } from './app.store'
...
imports: [
  StoreModule.forRoot(reducers, {}),
]

1 个答案:

答案 0 :(得分:4)

看起来你应该看看migration guide的ngrx v4。

v4改变了你为减压者注册的方式。

StoreModule.forRoot(reducers, {})有效,因为StoreModule.forRoot需要类型为ActionReducerMap<T, V>的对象。第二个参数:{} - 是初始状态。