【问题标题】:use formData with ngrx将 formData 与 ngrx 一起使用
【发布时间】:2022-06-17 20:23:50
【问题描述】:

我使用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 操作时出现错误:Argument of type \'{ this: any; }\' is not assignable to parameter of type \'{ item: Item; }\'.

addItem 操作代码是这样的:

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

那么我该如何解决这个错误???

  • 如果我的回答对您的问题有所帮助,我们非常欢迎您接受它作为解决方案,这样有类似问题的其他人可以更轻松地找到它。周末愉快

标签: angular ngrx


【解决方案1】:

您需要容纳您指定的有效负载 {item: Item} 以便调度

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

它需要一个键/值对,正如您在 createAction 函数的道具中指定的那样。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-06-24
    • 1970-01-01
    • 2017-11-08
    • 2018-08-11
    • 1970-01-01
    • 2018-09-08
    • 2020-06-02
    相关资源
    最近更新 更多