我试图弄清楚如何在最新版本的 Ngrx 中设置特定值。文档提到了如何在存储中递增/递减/重置值,但我没有看到任何有关如何动态设置值或如何将参数传递给减速器的示例。
这是我目前所拥有的,但我知道这是不正确的:
我的行动:
// TODO: properly implement action
export const setLabel = createAction('[Label Component] Set, props<{ addressField: string }>()');
Run Code Online (Sandbox Code Playgroud)
我的减速机:
export interface AppState {
addressField;
}
const _reducer = createReducer(
// initial state:
{ addressField: '' },
// TODO: update `addressField`:
on(setLabel, state => {
return {
...state
};
})
);
export function labelReducer(state, action) {
return _reducer(state, action);
}
Run Code Online (Sandbox Code Playgroud)
最后,我的组件:
// imports...
export class MyComponent implements OnInit {
constructor( private store: Store<AppState>,
private AddressService: AddressService) {
} …Run Code Online (Sandbox Code Playgroud)