Angular 5 http删除不工作
我正在尝试使用http.delete删除一篇文章,但Angular没有发出请求。我不知道少了什么 blog-service.ts:Angular 5 http删除不工作,angular,Angular,我正在尝试使用http.delete删除一篇文章,但Angular没有发出请求。我不知道少了什么 blog-service.ts: import { Injectable } from '@angular/core'; import { HttpClient } from '@angular/common/http'; import 'rxjs/add/operator/map'; const BASE_URL = 'http://localhost:8080'; [...] deleteA
import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import 'rxjs/add/operator/map';
const BASE_URL = 'http://localhost:8080';
[...]
deleteArticle(id) {
this.http.delete(`${BASE_URL}`+'/api/articles/'+ id)
}
编辑-blog.component.ts:
import { Component, OnInit } from '@angular/core';
import { FormBuilder, FormGroup, Validators } from '@angular/forms';
import { ActivatedRoute, Router } from "@angular/router";
import { BlogService } from "../../../services/blog.service";
[...]
onPostDelete() {
this.blogService.deleteArticle(this.urlParam.id)
}
ngOnInit() {
this.urlParam = this.activatedRoute.snapshot.params;
this.blogService.getArticle(this.urlParam.id)
.subscribe(data =>this.article = data);
}
edit-blog.component.html:
<form [formGroup]="createPostForm" (submit)="onPostSubmit()" autocomplete="off">
[...]
</form>
<button class="btn btn-outline-danger" (click)="onPostDelete()">Delete Post</button>
[...]
删除帖子
谁能告诉我少了什么?谢谢为了提出请求,您需要订阅如下
onPostDelete() {
this.blogService.deleteArticle(this.urlParam.id).subscribe((response) => {
console.log("deleted"));
});
}
并修改您的博客服务,如下所示:
deleteArticle(id) {
return this.http.delete(`${BASE_URL}`+'/api/articles/'+ id).map((response: Response) => response.json())
}
您需要订阅从
delete
返回的可观察对象。我尝试了此处建议的方法:,但它抛出错误“无法读取未定义的属性'subscribe'”@KirkLarkin yep我没有看到哦,返回!Thanks@Sajeetharan如果我需要在删除请求中发送requestBody
,该怎么办?