NGRX:创建多个减速器反应的"global"动作的正确方法?



正确纠正声明and and_action'的方法是什么。所有的还原器Actionreducermap可以对?

做出反应?

这个示例说明我的代码如何,我需要所有3个还原器(图像,标签,添加标签)才能对单个动作做出反应:

import { ActionReducerMap, createFeatureSelector } from '@ngrx/store';
import * as fromRoot from '../../../store';
import * as fromImages from './image.reducer';
import * as fromTags from './tag.reducer';
import * as fromAddedTags from './added-tag.reducer';
export interface AnalysisState {
    images: fromImages.State;
    tags: fromTags.State;
    addedTags: fromTags.State;
}
export interface State extends fromRoot.State {
    analysis: AnalysisState;
}
export const reducers: ActionReducerMap<AnalysisState> = {
    images: fromImages.reducer,
    tags: fromTags.reducer,
    addedTags: fromAddedTags.reducer
};
export const getAnlysisState = createFeatureSelector<AnalysisState>('analysis');

这是一个示例操作文件:

import { Action } from '@ngrx/store';
import { Tag } from '../../models/tag.model';
export const enum AddedTagActionTypes {
    ADD_TAG = '[ANALYSIS] - Add Tag'
}
export class AddTag implements Action {
    readonly type = AddedTagActionTypes.ADD_TAG;
    constructor(public payload: Tag) {}
}
export type AddedTagActions = AddTag;

这是一个示例还原器:

import { EntityState, EntityAdapter, createEntityAdapter } from '@ngrx/entity';
import * as fromAddedTag from '../actions/added-tag.actions';
import { Tag } from '../../models/tag.model';
export interface State extends EntityState<Tag> {}
export const adapter: EntityAdapter<Tag> = createEntityAdapter<Tag>({
    selectId: tag => tag.id
});
export const initialState: State = adapter.getInitialState({});
export function reducer(state = initialState, action: fromAddedTag.AddedTagActions): State {
    switch (action.type) {
        case fromAddedTag.AddedTagActionTypes.ADD_TAG: {
            return adapter.addOne(action.payload, state);
        }
        default: {
            return state;
        }
    }
}
const { selectIds, selectEntities, selectAll, selectTotal } = adapter.getSelectors();

export const getAllAddedTags = selectAll;

一个动作通过所有还原器。因此,如果您只是在所有3个还原器中对该操作的案例语句进行了案例语句,则将命中所有3个案例语句。

如果还没有针对还原器中的特定操作的案例语句,则将触发默认案例语句,这只是返回原始状态,使其未经修改。

这假设您不是懒惰的减少器。当然,仅在加载时才会触发一个还原器。

相关内容

最新更新