Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/csharp-4.0/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular 5 http删除不工作_Angular - Fatal编程技术网

Angular 5 http删除不工作

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

我正在尝试使用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';

[...]

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
,该怎么办?