我正在尝试使用 http.delete 删除一篇文章,但 Angular 没有发出请求。我不确定缺少什么。
博客服务.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)
}
编辑博客.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);
}
编辑博客.component.html:
<form [formGroup]="createPostForm" (submit)="onPostSubmit()" autocomplete="off">
[...]
</form>
<button class="btn btn-outline-danger" (click)="onPostDelete()">Delete Post</button>
有人能告诉我缺少什么吗?谢谢
为了提出请求,您需要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())
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)