如何在 Angular 中请求 api 删除操作

How to request api delete action in Angular

所以在我的 angular 项目中,我 API 调用了我的 workspace.service.ts 文件,并且我能够创建其他请求,如 getWorkspace 和 createWorkspace 等。但没有删除请求。

我是 API 调用或请求后端内容的新手,但我认为我需要调用一些删除操作。

所以我想在我的项目中做的是创建一个允许用户删除当前工作区的删除按钮。

export class Workspace {
    guid: string;
    name: string;
    description: string;
    type: WorkspaceType;
    userRole: WorkspaceRole;
    charts?: any[];
}
import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { environment } from 'src/environments/environment';
import { first, tap } from 'rxjs/operators';
import { Workspace } from 'src/app/shared/models/workspace.model'; //Code above

export class WorkspaceService {

  loadedWorkspaces: Workspace[]

  constructor(private http: HttpClient) { }

  getWorkspace(guid: string) {
    return this.http.get<Workspace>(`${environment.api.chart}/workspaces/${guid}`).pipe(first());
  }

  getUserWorkspaces() {
    return this.http.get<Workspace[]>(`${environment.api.chart}/workspaces`).pipe(tap(workspaces => this.loadedWorkspaces = workspaces),first());
  }

  createWorkspace(workspace: Workspace) {
    return this.http.post<Workspace>(`${environment.api.chart}/workspaces`, workspace).pipe(first());
  }
  deleteWorkspace(workspace: Workspace){
    //Delete request

  }

我不太确定要为 deleteWorkspace 做什么,我尝试从上面复制代码并将 HTTP 方法更改为 this.http.delete 但在 "workspace" 上出现错误,它说, "No overload match this call".

您可以简单地传递工作区 ID 来删除工作区。

 deleteWorkspace(workspace: Workspace) {
    return this.http.delete<Workspace>(`${environment.api.chart}/${workspace.guid}`);
  }

如果你想在删除请求中传递正文,这个问题可能对你有帮助: