简体   繁体   English

Angular 6 http.delete请求

[英]Angular 6 http.delete request

I cant seem to get my delete request to work. 我似乎无法让我的删除请求工作。 I have finished all of the get requests but now I'm stuck on delete and can't seem to wrap my head around it. 我已经完成了所有的get请求,但现在我仍然坚持删除,似乎无法绕过它。

The console.log'd URL is always correct and the delete request works fine via Postman. console.log'd URL始终正确,删除请求通过Postman正常工作。

Got any ideas? 有什么想法吗?

HTML HTML

<button class="button button3" (click)="delTicket()"><span class="fa fa-trash"></span></button>

TS TS

delTicket(){
    this.id = this.route.snapshot.params.id;
    this.ticketService.deleteTicket(this.id);
}

Service 服务

deleteTicket(id): Observable<Ticket[]>{
    console.log(this.apiUrl + id);
    return this.http.delete<Ticket[]>(this.apiUrl + id);
}

You need to call subscribe() inside your component, otherwise request wont get invoked 您需要在组件中调用subscribe() ,否则请求不会被调用

delTicket(){
    this.id = this.route.snapshot.params.id;
    this.ticketService.deleteTicket(this.id).subscribe((data)=>{
         console.log("success");
    });
}

You must call subscribe() or nothing happens. 你必须调用subscribe()或没有任何反应。 Just calling ticketService.deleteTicket(this.id) does not initiate the DELETE request. 只是调用ticketService.deleteTicket(this.id)不会启动DELETE请求。

  1. An HttpClient method does not begin its HTTP request until you call subscribe() on the observable returned by that method. 在您对该方法返回的observable上调用subscribe()之前,HttpClient方法不会开始其HTTP请求。 This is true for all HttpClient methods. 对于所有HttpClient方法都是如此。
  2. Calling subscribe(...) triggers execution of the observable and causes HttpClient to compose and send the HTTP request to the server. 调用subscribe(...)会触发observable的执行,并导致HttpClient组合并将HTTP请求发送到服务器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM