英文:
How to dispatch an action in NgRx with parameters?
问题
export enum ActionTypes {
UpdateData = '[Data] Update Data',
}
export class UpdateData implements Action {
readonly type = ActionTypes.UpdateData;
constructor(public payload: any) {}
}
// My Reducer:
export interface AppState {
data: any;
}
const initialState: AppState = {
data: null,
};
export function appReducer(state = initialState, action: Actions): AppState {
switch (action.type) {
case ActionTypes.UpdateData:
return {
...state,
data: action.payload,
};
default:
return state;
}
}
// My component:
import { Component } from '@angular/core';
import { Store } from '@ngrx/store';
import { AppState } from './app.state';
import { UpdateData } from './store/actions';
@Component({
selector: 'app-my-component',
template: `
<button (click)="updateData('Your Data Here')">Update Data</button>
`,
})
export class MyComponent {
constructor(private store: Store<AppState>) {}
updateData(data: any) {
this.store.dispatch(new UpdateData(data));
}
}
英文:
export enum ActionTypes {
UpdateData = '[Data] Update Data',
}
export class UpdateData implements Action {
readonly type = ActionTypes.UpdateData;
constructor(public payload: any) {}
}
My Reducer:
export interface AppState {
data: any;
}
const initialState: AppState = {
data: null,
};
export function appReducer(state = initialState, action: Actions): AppState {
switch (action.type) {
case ActionTypes.UpdateData:
return {
...state,
data: action.payload,
};
default:
return state;
}
}
My component:
import { Component } from '@angular/core';
import { Store } from '@ngrx/store';
import { AppState } from './app.state';
import { UpdateData } from './store/actions';
@Component({
selector: 'app-my-component',
template: `
<button (click)="updateData()">Update Data</button>
`,
})
export class MyComponent {
constructor(private store: Store<AppState>) {}
updateData() {
// How do I pass parameters here to the action?
this.store.dispatch(new UpdateData(/* What to pass here? */));
}
}
My question is, how can I pass parameters to the UpdateData action when dispatching it from the MyComponent? For example, let's say I want to update the data with a specific object or a string. How should I modify the updateData() function to achieve this?
I would greatly appreciate any insights or examples on how to handle this scenario properly. Thank you!
答案1
得分: 1
由于 payload
被定义为 any
,您可以将任何内容传递给它,但这可能会导致无效的状态。我的第一个建议是对其进行类型定义。
但是,回答您的问题:
this.store.dispatch(new UpdateData({ propertyName: parameterValue });
请参阅 https://timdeschryver.dev/blog/you-should-take-advantage-of-the-improved-ngrx-apis#actions 了解 NgRx 中“新”的和改进的动作语法。
英文:
Since payload
is typed as any
you can pass everything to it, but that probably will result in an invalid state. My first tip is to make it typed.
But to answer your question:
this.store.dispatch(new UpdateData({ propertyName: parameterValue });
See https://timdeschryver.dev/blog/you-should-take-advantage-of-the-improved-ngrx-apis#actions for the "new" and improved syntax of action in NgRx.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论