Angular - 使用具有不同 ID 属性 名称的通用基础服务时出现 400 错误

Angular - 400 error when using a generic base service with different id property name

我在选择给定项目时使用的是带有 id 属性 名称的基本 class 服务,但我有其他组件使用不同的 属性 个名字。

对于使用 'code' 而不是 'id' 的自定义组件,当我单击更新项目(PUT 方法)时,控制台给我一个 400 错误,并显示一个错误的请求 URL:

http://example.com/api/items/代码

而不是

http://example.com/api/items/C002

显示的有效负载是正确的:> { code:"C002", name "Item 2" }

条目class如下:

export interface Item {
    code: string;
    name: string;
    }

组件方法:

updateItem(form: FormGroup) {
    this.route.params.subscribe(params => {
      form.value.id = params['code'];

      this.itemService.update('code', form.value)
        .subscribe(res => {
          this.router.navigate(['/list']);
        });
    });
}

和基础 class 服务:

public update(id: string, dto: INDTO): Observable<OUTDTO> {
    return this.http.put<OUTDTO>(`/${this.controllerName}/${id}`, dto, this.endPointFactory.getRequestHeaders());
}

我做错了什么?

服务函数调用有点问题。不要传递硬编码值 code,八个应该传递 form.value.idparams['code'].

Modified code is as

updateItem(form: FormGroup) {
    this.route.params.subscribe(params => {
      form.value.id = params['code'];
      this.itemService.update(form.value.id, form.value)
        .subscribe(res => {
          this.router.navigate(['/list']);
        });
    });
}