【问题标题】:Angular 7 PATCH method to add element to a listAngular 7 PATCH 方法将元素添加到列表
【发布时间】:2019-07-08 03:39:59
【问题描述】:

我应该如何编写允许我在数组中添加和删除数组项的 PATCH 方法?

物品类:

export class ItemClass {
constructor(public person: string, public name: string, public quantity: number, public price: number){}
}

菜单模型:

    import { ItemClass } from './item.model';

    export class MenuModel {


    id: number;
    name: string;
    items: ItemClass[];

    constructor( id: number,  name: string, items: ItemClass[]){

         this.id = id;
         this.name = name;
         this.items = items;
    }
}

我有一个菜单组件和一个菜单服务。我需要一个补丁方法,它可以将元素添加到 Menu 内的 ItemClass[] 数组中,并且也能够删除它们。

API 方法如下:

   @PATCH
   @Path("/add/{menuId}")
   public void removeMenuItem(
            @PathParam("menuId") final int menuId,
            final Item item) {  // Item represents the Request Body
      final List<Item> items = this.menuRepository.get(menuId).getItems();
      items.add(item);
   }

(来自https://stackoverflow.com/a/54679303/479251

【问题讨论】:

  • 不清楚你在问什么
  • 我需要一个PATCH API方法,它可以添加和删除ItemClass[]数组的元素,它是Menu的一个参数。
  • 我认为您需要回到指示您这样做的人那里,并要求更多解释。
  • 我可能应该提供更多数据。它已经是一个很大的代码库,我有很多东西在工作(GET、POST、DELETE 请求)我有更多的类,但它们与此无关。
  • 数组在任何地方都是一个数组。您唯一需要的是在使用推送或切片之前检查是否为空,例如if (!mymenu.items) {mymenu.items=[]}

标签: angular typescript rest api


【解决方案1】:

在后端补丁方法的其他详细信息之后,这是一个(粗略的)示例,说明如何做到这一点。

您还没有为 Angular 指定版本,所以我假设您使用的是最新版本 (7) 和 HttpClient

import { HttpClient } from '@angular/common/http'; 
/* ... */ 
export class YourComponentOrService {

  // note : you will need to add HttpClientModule to the application module 'imports' and 'providers' sections
  constructor(private http: HttpClient) {} 

  /* ...*/ 

  // call this method when you want to add an item
  public addItem(menuID: number, itemToAdd: ItemClass): void { 
    console.log('sending patch request to add an item');

    this.http.patch(`example.com/add/{menuId}`, itemToAdd).subscribe(
      res => { 
        console.log('received ok response from patch request');
      },
      error => {
        console.error('There was an error during the request');
        console.log(error);
      });

    console.log('request sent. Waiting for response...');

  }

  // as I could see on the linked Q&A, the delete method will be very similar
  // only the url will use 'remove' instead of 'add', along with the item name, and no body.

  /* .... */
}     

当然,这是帮助您入门、适应您的需求和 Angular 应用的整体架构的基本“概念证明”。

如您所见,我所做的只是读取 API 端点,并相应地使用来自 HttpClient Angular 服务的 patch 方法,以及预期的 url 和内容。

现在,您必须添加逻辑以使用正确的参数发送请求。

【讨论】:

  • 好的,我试试那个。我的菜单组件方法应该是什么样的?我将绑定到(单击)的那个?谢谢你,我会在得到有关组件最终方法的答案后立即尝试。是的,我使用的是最新版本。
  • @soulzap 我真的不知道如何回答这个问题,我不知道您的页面应该是什么样子,行为是什么等等......这取决于您的规格。基本上,你应该有一个方法,当点击时,创建/读取要添加的“项目”对象,并调用我上面写的方法。此外,您可能希望在请求结束后刷新显示的项目列表。
  • 也许你可以使用StackBlitz 来创建看起来像你想做的东西
  • 我只需要创建一个在单击按钮时将激活的函数。该函数实现 PATCH 方法。希望你明白我想说什么。例如,这是我的 getMenu GET 方法: getMenu(id: number): Observable{ const url = ${this.menusUrl}/${id};返回 this.http.get(url) } ____ getMenu(): void { const id = +this.route.snapshot.paramMap.get('id'); this.menuService.getMenu(id) .subscribe(menu => this.menu = menu); } _____ 后者是按钮点击事件的函数。
  • google-chrome --disable-web-security --user-data-dir 以这种方式启动 chrome,解决了它。它仅用于开发目的,所以对我来说已经足够了。谢谢你所做的一切!
猜你喜欢
  • 2019-04-27
  • 1970-01-01
  • 2014-06-16
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多