将formdata与ngrx一起使用

发布于 2025-02-08 17:52:06 字数 743 浏览 2 评论 0原文

我使用ngrx并尝试提交我的FormData,这是我的代码:

formData = new FormData();

onSubmit() {
   this.formData.set('name', this.userForm.get('name')?.value);
   this.formData.set('description', this.userForm.get('description')?.value);
   this.formData.set('price', this.userForm.get('price')?.value);
   this.formData.set('category', this.userForm.get('category')?.value);
   this.formData.set('available', this.userForm.get('available')?.value);
    this.store.dispatch(AddItem({this.formData} ))
 
  }

当我尝试派遣AddItem操作时,我有一个错误:类型'{this:any; }'不能分配给类型的参数'{item:item; 。

export const AddItem = createAction(
  ItemActionsNames.AddItems,
  props<{ item: Item }>()
);

I use ngrx and try to submit my formData and this is my code :

formData = new FormData();

onSubmit() {
   this.formData.set('name', this.userForm.get('name')?.value);
   this.formData.set('description', this.userForm.get('description')?.value);
   this.formData.set('price', this.userForm.get('price')?.value);
   this.formData.set('category', this.userForm.get('category')?.value);
   this.formData.set('available', this.userForm.get('available')?.value);
    this.store.dispatch(AddItem({this.formData} ))
 
  }

when I try to dispatch addItem action I have an error : Argument of type '{ this: any; }' is not assignable to parameter of type '{ item: Item; }'.

and the addItem action code is this :

export const AddItem = createAction(
  ItemActionsNames.AddItems,
  props<{ item: Item }>()
);

so how I can solve this error ???

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

溺深海 2025-02-15 17:52:06

您需要适应已指定的有效载荷{item:item},以便调度将

this.store.dispatch(AddItem({item: this.formData}))

期望与您在创建功能中指定的道具中指定的键/值对。

You need to accomodate the payload you've specified {item: Item} so the dispatch will be

this.store.dispatch(AddItem({item: this.formData}))

It expects a key/value pair as you've specified in your props in the createAction function.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文