在下面的代码中,我有一个服务1:uploadAttachment方法,实际上正在进行http post调用,并且该方法(uploadAttachment)是从另一个服务2调用的。在服务2中,我总是得到服务1的实例未定义。因此,呼叫服务1失败。
服务1:
@Injectable({
providedIn: 'root'
})
export class Service1 {
constructor(private http: HttpClient) {}
uploadAttachments(formData: any): any {
const headers = new HttpHeaders();
headers.append('Content-Type', 'multipart/form-data');
headers.append('Accept', 'application/json');
return this.http.post(this.baseURL + 'Data/UpdateInsertAttachments', formData,
{headers: headers});
}
}
服务2:
@Injectable({
providedIn: 'root'
})
export class Service2 {
constructor(private service: Service1) {
fileChange(event) {
const formData: FormData = new FormData();
// some code
this.service.uploadAttachments(formData).subscribe(
resp => {
if (resp === -1 ) {
alert('Error occured while uploading the attachment');
return;
} else {
// some code
}
},
(error) => {
console.log('POST ERROR in method uploadAttachments: ' + error.error);
});
}
}
答案 0 :(得分:0)
由于Service1在其签名中需要一个HttpClient,因此您必须为其提供一个