来自 ngrx/store 的 Reducer 没有启动
Reducer from ngrx/store does not fire
我在 "@angular/core": "4.4.3"
和 "@ngrx/store": "4.0.3"
项目中使用 combineReducers
,但我遇到了 reducer 在发送操作后没有被拾取的问题。
这可能只是我对 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, {}),
]
看来您应该看一下 ngrx v4 的 migration guide。
v4 改变了你注册减速器的方式。
StoreModule.forRoot(reducers, {})
有效,因为 StoreModule.forRoot
需要类型 ActionReducerMap<T, V>
的对象。第二个参数:{} - 是初始状态。
我在 "@angular/core": "4.4.3"
和 "@ngrx/store": "4.0.3"
项目中使用 combineReducers
,但我遇到了 reducer 在发送操作后没有被拾取的问题。
这可能只是我对 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, {}),
]
看来您应该看一下 ngrx v4 的 migration guide。
v4 改变了你注册减速器的方式。
StoreModule.forRoot(reducers, {})
有效,因为 StoreModule.forRoot
需要类型 ActionReducerMap<T, V>
的对象。第二个参数:{} - 是初始状态。