我正在尝试在Angular版本7上设置服务,但是我在res.json()
上遇到问题,该问题引发了错误Property 'json' does not exist on type 'Object'
。这是我的服务代码:
import {Injectable} from '@angular/core';
import {HttpClient} from '@angular/common/http';
import {Observable} from "rxjs";
import 'rxjs/add/operator/map';
import { map } from 'rxjs/operators';
import {
SVCParameters,
SVCResult
} from "./types";
const SERVER_URL: string = 'api/';
@Injectable()
export class IrisService {
constructor(private http: HttpClient) {
}
public trainModel(svcParameters: SVCParameters): Observable<SVCResult> {
return this.http.post(`${SERVER_URL}train`, svcParameters).pipe(map(res => res.json()));
}
}
以下是我的types.ts代码:
export class SVCParameters {
C: number = 2.0;
}
export class SVCResult{
accuracy: number;
}
答案 0 :(得分:0)
Angular 7 HttpClientModule,默认情况下返回响应为 JSON
,无需映射响应,因为res.json()
只需删除管道运算符即可。一切都会正常。
import {Injectable} from '@angular/core';
import {HttpClient} from '@angular/common/http';
import {Observable} from "rxjs";
import 'rxjs/add/operator/map';
import { map } from 'rxjs/operators';
import {
SVCParameters,
SVCResult
} from "./types";
const SERVER_URL: string = 'api/';
@Injectable()
export class IrisService {
constructor(private http: HttpClient) {
}
public trainModel(svcParameters: SVCParameters): Observable<SVCResult> {
return this.http.post(`${SERVER_URL}train`, svcParameters);
}
}