因此,我在GitHub和StackOverflow上经历了大多数类似的问题。
在my issue.component.ts
文件中,我已将下拉菜单的值绑定到变量issueInformation
。现在,我需要将此数据发送到服务器,并且我正在使用后请求。
这是我的issue.component.ts
文件:
import { Component, OnInit, DoCheck } from '@angular/core';
import { IssuesService } from '../issues.service';
import { HttpClientModule } from '@angular/common/http';
import { Http } from '@angular/http';
@Component({
selector: 'app-issue',
templateUrl: './issue.component.html',
styleUrls: ['./issue.component.css']
})
export class IssueComponent implements OnInit, DoCheck {
issueInfo: any[] = [];
issueInformation: any;
httpClient: any;
http: any;
constructor(private issue: IssuesService) { }
ngDoCheck(): void {
console.log('issue:', this.issueInformation);
}
ngOnInit() {
this.getIssues();
}
getIssues() {
console.log('issue::', this.issueInformation);
this.issue.allIssues().
subscribe(
data2 => {
this.issueInfo = data2.Issues;
},
err => console.log(err),
() => console.log('complete')
);
}
}
因为我是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)
我尝试了以下操作:
constructor{private issue: IssuesService,private http: Http}
但这对我不起作用。
我还尝试了将issues.service.ts
文件中的doPOST()函数移位,但是无法在issue.component.ts
文件中调用它。
这是issues.service.ts
文件:
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()));
}
}
有人可以告诉我怎么了吗?如果我从一开始就做错了,那么请告诉我如何将issueInformation
的值发送回服务器?
答案 0 :(得分:0)
花了整整一天时间试图解决这个问题之后,我找到了解决方案:
constructor(private issue: IssuesService, private httpClient: HttpClient) { }
添加此内容后,可以将POST请求与issue.component.ts
放在同一文件中,如下所示:
postIssue() {
this.httpClient.post('http://localhost:8080/uploadFile',
{issueInformation: this.issueInformation}).subscribe((data: any) =>
console.log(data));
}