无法发送POST请求。无法读取未定义的属性“post”

无法发送POST请求。无法读取未定义的属性“post”,post,angular7,Post,Angular7,所以,我已经讨论了GitHub和StackOverflow上的大多数类似问题 在我的issue.component.ts文件中,我将下拉菜单的值绑定到变量issueInformation。现在,我需要将这些数据发送到服务器,并使用post请求 这是我的issue.component.ts文件: 因为,我是Angular的新手,我尝试了一个POST请求示例,并在getIssues函数之后将其添加到此文件中: doPOST() { console.log("POST"); let url =

所以,我已经讨论了GitHub和StackOverflow上的大多数类似问题

在我的issue.component.ts文件中,我将下拉菜单的值绑定到变量issueInformation。现在,我需要将这些数据发送到服务器,并使用post请求

这是我的issue.component.ts文件:

因为,我是Angular的新手,我尝试了一个POST请求示例,并在getIssues函数之后将其添加到此文件中:

doPOST() {
  console.log("POST");
  let url = `${this.apiRoot}/post`;
  this.http.post(url, {moo:"foo",goo:"loo"}).subscribe(res => console.log(res.json()));
}
我还尝试将change=doPOST$event.target.value添加到HTML中的下拉选择行中

我得到的错误是:

AppComponent.html:1 ERROR TypeError: Cannot read property 'post' of undefined
    at IssueComponent.getIssues (issue.component.ts:30)
    at IssueComponent.ngOnInit (issue.component.ts:24)
    at checkAndUpdateDirectiveInline (core.js:24489)
    at checkAndUpdateNodeInline (core.js:35151)
    at checkAndUpdateNode (core.js:35090)
    at debugCheckAndUpdateNode (core.js:36112)
    at debugCheckDirectivesFn (core.js:36055)
    at Object.eval [as updateDirectives] (AppComponent.html:7)
    at Object.debugUpdateDirectives [as updateDirectives] (core.js:36043)
    at checkAndUpdateView (core.js:35055)
我尝试了以下方法: 构造函数{私有问题:IssuesService,私有http:http} 但这对我不起作用

我还尝试在issue.service.ts文件中移动doPOST函数,但无法在issue.component.ts文件中调用它

以下是issues.service.ts文件:


有人能告诉我出了什么问题吗?如果我从一开始就做错了,那么请告诉我如何将issueInformation的值发送回服务器?

在花了一整天的时间试图解决这个问题后,我找到了解决方案:

构造函数私有问题:IssueService,私有httpClient:httpClient{}

添加后,POST请求可以放在与issue.component.ts相同的文件中,如下所示:

postIssue() {
      this.httpClient.post('http://localhost:8080/uploadFile', 
      {issueInformation: this.issueInformation}).subscribe((data: any) => 
      console.log(data));
  }
import { Injectable } from '@angular/core';
import { HttpClient, HttpHeaders } from '@angular/common/http';
import {Response, Headers, RequestOptions} from '@angular/http';
import { Observable} from 'rxjs';
import { IssueComponent } from './issue/issue.component';


@Injectable({
  providedIn: 'root'
})


export class IssuesService {

  url = 'http://localhost:8080/issueTypes';
_baseUrl = 'http://localhost:8080';
  constructor(private http: HttpClient) { }

  allIssues(): Observable<any> {
    return this.http.get(this.url);
  }
 doPost() {
      console.log('POST');
      const url = '/api/issue';
      this.http.post(url, {moo: 'foo', goo: 'loo'}).subscribe(res => console.log(res.json()));
                                          }
}
postIssue() {
      this.httpClient.post('http://localhost:8080/uploadFile', 
      {issueInformation: this.issueInformation}).subscribe((data: any) => 
      console.log(data));
  }