我有下面这段代码工作正常。
public createData(data:Data):Observable<any>{
console.log('contacting server at '+this.API_URL +this.NEW_DATA_URL +" with data "+data+ " with httpOptions "+httpOptions.withCredentials + ","+httpOptions.headers );
let newData = new Data(data)
let body = JSON.stringify(newQuestion);
//I WANT TO MOVE THE CODE BELOW INTO A FUNCTION BUT AM GETTING COMPILATION ERROR
this.loaderService.show();
return this.http.post(this.NEW_QUESTION_URL,body,httpOptions)
.pipe(tap(response => {
let httpEvent = <HttpEvent<any>>response;
if(httpEvent.type === HttpEventType.Response)
{
console.log('response from backend service:', response);
return result;
}
else {
console.log("not an http response")
return response;
}
})
,catchError(this.handleError)
,finalize(()=> this.loaderService.hide())); //error handler if Observable fails
}
正如我可以重用的代码的某些部分,我想创建一个函数,但现在我得到编译错误。 我无法找出什么是错。
新功能
private sendMessage(url:string, body:any,httpOptions:any){
this.loaderService.show();
return this.http.post(url,body,httpOptions)
.pipe(tap(response => {
let httpEvent = <HttpEvent<any>>response; //ERROR HERE - 'ArrayBuffer' cannot be converted to type 'HttpEvent<any>'.
if(httpEvent.type === HttpEventType.Response)
{
console.log('response from backend service:', response);
let result = <HttpResponse<any>>response;
return result;
}
else {
console.log("not an http response")
return response;
}
})
,catchError(this.handleError)
,finalize(()=> this.loaderService.hide())); //error handler if Observable fails
}
ERROR是ERROR in src/app/web-to-backend-interface.service.ts(264,27): error TS2352:
Type 'ArrayBuffer' cannot be converted to type 'HttpEvent<any>'.
Type 'ArrayBuffer' is not comparable to type 'HttpUserEvent<any>'.
Property 'type' is missing in type 'ArrayBuffer'.
http.post
有几个重载的方法,其中一个返回可观察- angular.io/api/common/http/HttpClient#post。 如何使角使用重载版本,它返回Observable<T>
我改变了代码,增加responseType:'json'
,但没有工作要么
const httpOptions = {
headers: new HttpHeaders({ 'Content-Type': 'application/json' }),
withCredentials: true,
observe: 'response' as 'response',
responseType: 'json'
};
let observable:Observable<HttpEvent<any>> = this.http.post(url,body,httpOptions)
return observable.pipe(tap((httpEvent:HttpEvent<any>) => {....}
我看到的错误
ERROR in src/app/web-to-backend-interface.service.ts(80,71): error TS2345: Argument of type '{ headers: HttpHeaders; withCredentials: boolean; observe: "response"; responseType: string; }' is not assignable to parameter of type '{ headers?: HttpHeaders | { [header: string]: string | string[]; }; observe?: "body"; params?: Ht...'.
Types of property 'observe' are incompatible.
Type '"response"' is not assignable to type '"body"'.
src/app/web-to-backend-interface.service.ts(111,52): error TS2345: Argument of type '{ headers: HttpHeaders; withCredentials: boolean; observe: "response"; responseType: string; }' is not assignable to parameter of type '{ headers?: HttpHeaders | { [header: string]: string | string[]; }; observe?: "body"; params?: Ht...'.
Types of property 'observe' are incompatible.
Type '"response"' is not assignable to type '"body"'.
src/app/web-to-backend-interface.service.ts(195,73): error TS2345: Argument of type '{ headers: HttpHeaders; withCredentials: boolean; observe: "response"; responseType: string; }' is not assignable to parameter of type '{ headers?: HttpHeaders | { [header: string]: string | string[]; }; observe?: "body"; params?: Ht...'.
src/app/web-to-backend-interface.service.ts(215,72): error TS2345: Argument of type '{ headers: HttpHeaders; withCredentials: boolean; observe: "response"; responseType: string; }' is not assignable to parameter of type '{ headers?: HttpHeaders | { [header: string]: string | string[]; }; observe?: "body"; params?: Ht...'.
src/app/web-to-backend-interface.service.ts(250,54): error TS2345: Argument of type '{ headers: HttpHeaders; withCredentials: boolean; observe: "response"; responseType: string; }' is not assignable to parameter of type '{ headers?: HttpHeaders | { [header: string]: string | string[]; }; observe?: "body"; params?: Ht...'.
src/app/web-to-backend-interface.service.ts(290,9): error TS2322: Type 'Observable<ArrayBuffer>' is not assignable to type 'Observable<HttpEvent<any>>'.
Type 'ArrayBuffer' is not assignable to type 'HttpEvent<any>'.
Type 'ArrayBuffer' is not assignable to type 'HttpUserEvent<any>'.
Property 'type' is missing in type 'ArrayBuffer'.
src/app/web-to-backend-interface.service.ts(296,59): error TS2552: Cannot find name 'response'. Did you mean 'Response'?
为什么做的工作代码的单独功能使得非编译? 什么是我的错?