Angular 从服务中发出POST请求

Angular 从服务中发出POST请求,angular,post,Angular,Post,我正在努力从服务创建AngularV7中的POST请求。我的服务类中已经有一些get请求从api中提取数据。现在我需要发布数据,但我无法理解格式/语法(对于angular来说仍然是新的) 在VSCode中,我可以看到它说类型上不存在属性“subscribe” “无效”。 这是服务 shipping.service.ts import { Injectable } from '@angular/core'; import { HttpClient } from '@angular/common/h

我正在努力从服务创建AngularV7中的POST请求。我的服务类中已经有一些get请求从api中提取数据。现在我需要发布数据,但我无法理解格式/语法(对于angular来说仍然是新的)

在VSCode中,我可以看到它说类型上不存在
属性“subscribe”
“无效”。

这是服务

shipping.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.");
  }
}
从'@angular/core'导入{Injectable};
从'@angular/common/http'导入{HttpClient};
从“rxjs”导入{Observable};
从“rxjs/operators”导入{catchError,tap,map};
从'@angular/router'导入{ActivatedRoute};
从“../interfaces/Ziptastic”导入{Ziptastic};
从“../interfaces/return Shipping”导入{ReturnShipping};
从'@angular/common/http'导入{HttpHeaders};
常量httpOptions={
标题:新的HttpHeaders({
“内容类型”:“应用程序/json”
})
};
@注射的({
providedIn:'根'
})
出口级航运服务{
private shipmentCreation='api/ReturnShipmentQueues';
构造函数(私有http:HttpClient){}
}
提交(rShip:退货装运){
this.http.post(this.shipmentCreation、rShip、httpOptions)
.订阅(
data=>{console.log('AllthePost'+JSON.stringify(data));},
err=>{console.log(“发生错误”);}
);
}
私有handleError(handleError:any):导入(“rxjs”).OperatorFunction{
抛出新错误(“方法未实现”);
}
}

您不应该在您的服务中订阅,而应该在组件中订阅,并且
从您的服务返回http.post

 return this.http.post(this.shipmentCreation, rShip, httpOptions)
您的组件代码应该是

this.shipmentServic.submitShipment(shipObj).subscribe(response => {

});

您不应该在您的服务中订阅,而应该在组件中订阅,并从您的服务返回http.post

 return this.http.post(this.shipmentCreation, rShip, httpOptions)
您的组件代码应该是

this.shipmentServic.submitShipment(shipObj).subscribe(response => {

});
/**
*构造一个POST请求,将主体解释为JSON并返回。
*
*@返回身体作为“对象”的“可观察的”。
*/
post(url:string,body:any | null,options?:{
标题?:HttpHeaders;
观察?:“身体”;
参数?:HttpParams;
reportProgress?:布尔值;
responseType?:'json';
withCredentials?:布尔值;
}):可见;
这很奇怪,因为http.post返回observable,iis可以订阅它

/**
*构造一个POST请求,将主体解释为JSON并返回。
*
*@返回身体作为“对象”的“可观察的”。
*/
post(url:string,body:any | null,options?:{
标题?:HttpHeaders;
观察?:“身体”;
参数?:HttpParams;
reportProgress?:布尔值;
responseType?:'json';
withCredentials?:布尔值;
}):可见;

这很奇怪,因为http.post返回observable,iis可以订阅它

你的rxjs版本是什么?这是新问题还是新答案?你的rxjs版本是什么?这是新问题还是新答案?