如何在NgRx中带参数调度一个动作?

huangapple go评论49阅读模式
英文:

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 = &#39;[Data] Update Data&#39;,
}

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 &#39;@angular/core&#39;;
import { Store } from &#39;@ngrx/store&#39;;
import { AppState } from &#39;./app.state&#39;;
import { UpdateData } from &#39;./store/actions&#39;;

@Component({
  selector: &#39;app-my-component&#39;,
  template: `
    &lt;button (click)=&quot;updateData()&quot;&gt;Update Data&lt;/button&gt;
  `,
})
export class MyComponent {
  constructor(private store: Store&lt;AppState&gt;) {}

  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.

huangapple
  • 本文由 发表于 2023年7月24日 20:10:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/76754370.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定