我正在尝试使用http.delete删除文章,但Angular没有发出请求。我不确定缺少什么。
博客-service.ts:
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);
}
编辑blog.component.html:
<form [formGroup]="createPostForm" (submit)="onPostSubmit()" autocomplete="off">
[...]
</form>
<button class="btn btn-outline-danger" (click)="onPostDelete()">Delete Post</button>
有人可以告诉我缺少什么吗?感谢
答案 0 :(得分:4)
要发出请求,您需要 subscribe
,如下所示
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())
}