我正在努力从服务的Angular v7中创建POST请求。我的服务类中已经有一些get请求,这些请求从我的api中提取数据。现在,我需要发布数据,但是我无法弄清楚格式/语法(对angular来说还是很新的)。
在VSCode中,我可以看到它说Property 'subscribe' does not exist on type
'void'.
这是服务
Shipment.service.ts
import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { Observable } from 'rxjs';
import { catchError, tap, map } from 'rxjs/operators';
import { ActivatedRoute } from '@angular/router';
import { Ziptastic } from '../interfaces/ziptastic';
import { ReturnShipment } from '../interfaces/return-shipment';
import { HttpHeaders } from '@angular/common/http';
const httpOptions = {
headers: new HttpHeaders({
'Content-Type': 'application/json'
})
};
@Injectable({
providedIn: 'root'
})
export class ShipmentService {
private shipmentCreation = 'api/ReturnShipmentQueues';
constructor(private http: HttpClient) { }
}
submitShipment(rShip: ReturnShipment) {
this.http.post(this.shipmentCreation, rShip, httpOptions)
.subscribe(
data => {console.log('AllthePost' + JSON.stringify(data));},
err => {console.log("error occurred");}
);
}
private handleError(handleError: any): import("rxjs").OperatorFunction<any[], any> {
throw new Error("Method not implemented.");
}
}
答案 0 :(得分:0)
/**
* Construct a POST request which interprets the body as JSON and returns it.
*
* @return an `Observable` of the body as an `Object`.
*/
post(url: string, body: any | null, options?: {
headers?: HttpHeaders;
observe?: 'body';
params?: HttpParams;
reportProgress?: boolean;
responseType?: 'json';
withCredentials?: boolean;
}): Observable<Object>;
这很奇怪,因为http.post返回observable,并且有可能订阅它