即使我对问题没有完全的了解,我也会尝试解决几个问题。
为什么要放
+那里?它将以数字表示形式转换变量,这就是您想要的吗?该
get()调用的返回值是
string | null
const id = +this.route.snapshot.paramMap.get('id');在这里你打电话
menuService.patchAdd跟
this.item,但我想你想创建一个新的
Item使用的输入值实例
name和
price。
// Angular(click) = patchItAdd(itemName.value, itemPrice.value)// JavapatchItAdd(name: string, price: number){ ... this.menuService.patchAdd(id, this.item)我的假设正确吗?
我看到的另一个问题是您将传递了
id,它应该
string(或者
number在您的情况下,因为您使用对其进行了转换
+)
patchAdd
const id = +this.route.snapshot.paramMap.get('id');this.menuService.patchAdd(id, this.item)该
patchAdd函数期望a
MenuModel,这是一个复杂的对象。
怎么样?您确定这是您想要的吗?
让我们
patchItAdd接受
menu.id价值
(click) = "patchItAdd(menu.id, itemName.value, itemPrice.value)"
现在更新方法
// Now this method accept the MenuModel#id propertypatchItAdd(menuId: number, name: string, price: number){ if (!name) { return; } const trimmedName = name.trim(); // Construct a new ItemClass instance. // You need to take care by yourself of the "person" property and the "quantity" property const newItem = new ItemClass("", trimmedName, 0, price) this.menuService.patchAdd(menuId, newItem).subscribe(item => { this.items.push(item); });}还更新
patchAdd方法以接受菜单ID,而不是完整的菜单ID。
MenuModel
// Now this method accepts the menu ID patchAdd(menuId: number, itemToAdd: ItemClass): Observable<any> { const url = `${this.menusUrl}/add/${menuId}` return this.http.patch(url, itemToAdd, httpOptions)}在后端添加一个无参数的构造函数
Item
public Item() { }


