我有一个表格,其中的数据已发布到网络api。为此编写了一个服务,并将该服务注入onSubmit函数内的form组件中。我收到An argument for 'payload' was not provided
的错误。我看不出问题可能在哪里?到目前为止,我的代码是:
service.ts
postFormData(payload) {
this.http.post(this.serviceApiUrl, payload, { headers: new HttpHeaders().set("Content-Type", "application/json")});
}
component.ts
constructor(private service: nowService,
private appComponent: AppComponent,
private router: Router,
private http: HttpClient,
) {
}
onSubmit() {
if(this.serviceForm.invalid) {
this.serviceForm.setErrors({ ...this.serviceForm.errors, 'required': true });
return;
}
this.service.postFormData().subscribe((response: any) => {
console.log(response);//On success response
this.router.navigate(['confirmation']);
}, (errorResponse: any) => {
console.log(errorResponse); //On unsuccessful response
});
}
}
答案 0 :(得分:2)
在调用服务时,需要从缺少的组件中传递参数有效负载。从component.ts传递参数
this.service.postFormData(payload).subscribe((response: any)
编辑
您的参数应为
this.service.postFormData(this.serviceForm.value).subscribe((response: any)
并且在订阅之前,您应从服务中以
形式返回ObservablepostFormData(payload) {
//add return//
return this.http.post(this.serviceApiUrl, payload, { headers: new HttpHeaders().set("Content-Type", "application/json")});
}
答案 1 :(得分:0)
使用如下所示的form.value,
this.service.postFormData( serviceForm.value )。subscribe(....);
答案 2 :(得分:0)
generateProductImages(campaignId, prod_id) {
return this.http.patch(CAMPAIGNS_URL.GENERATE_PRODUCT_IMAGES(campaignId, prod_id), {})
}