我在文件中声明操作
// action.ts
export const FILTER_ROLES = "FILTER_ROLES"
// this action will be handled in the effect
export class FilterRoles implements Action {
readonly type = FILTER_ROLES
constructor(public query: any) { }
}
export const FILTERED_ROLES = "FILTERED_ROLES"
// this one will modify the reducer
export class FilteredRoles implements Action {
readonly type = FILTERED_ROLES
constructor(public values: Array<any>, public total: number) { }
}
效果文件看起来像这样,(效果将调用后端)
@Injectable()
export class RoleEffects {
@Effect() filter_roles = this.actions$
.ofType(FILTER_ROLES)
.flatMap((action: FilterRoles) => {
return
backendCall()
.mergeMap(({ values, total }) => {
return [
new FilteredRoles(values, total)
]
}).catch((err: Error) => { console.log("sss") })
}
减速器将修改您商店的当前状态
export function RoleReducer(state: Roles = { values: [], total: 0 }, action: RoleActions) {
switch (action.type) {
case FILTERED_ROLES:
return Object.assign({}, state, { values: action.values, total: action.total })
default:
return state
}
}
在我的模块声明中,您应该同时声明效果和减速器操作
const EffectModules = [
EffectsModule.run(RoleEffects)
....
....
]
在我的模块的导入中,我将声明所有减速器和效果
@NgModule({
imports: [
StoreModule.provideStore({roles: RoleReducer,
... // all the other reducers
}),
...EffectModules,
]
})
我希望这段代码对你有帮助