转换为函数时无法编译代码
unable to compile a code when converted into a function
我有以下代码可以正常工作。
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 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
有几个重载方法,其中之一是 return Observable - angular.io/api/common/http/HttpClient#post。如何让 Angular 使用 returns 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'?
为什么为一个工作代码创建一个单独的函数会使其不可编译?我的错误是什么?
虽然我无法回答为什么你现在会收到 TS 错误,而你以前没有,但我可以建议你如何摆脱它(假设代码在你重构之前是有效的) .如果你在声明中给 response
一个类型,那么它不会得到你必须尝试映射的假定类型:
.pipe(tap((response: HttpEvent<any>) => {
let httpEvent = response; // No need for type-casting here anymore.
我能够解决它,但现在我的问题多于答案。如果有人能向我解释这种行为,我将不胜感激。
我正在使用以下 httpOptions
对象
const httpOptions = {
headers: new HttpHeaders({ 'Content-Type': 'application/json' }),
withCredentials: true,
observe: 'response' as 'response',
};
在代码中的某些地方按如下方式使用它。
return this.http.post(this.SIGNUP_USER_URL,body,httpOptions)
这有效。
然后我想到将 http.post
更改为函数 sendMessage
(如问题中所述)并仅在一次地方替换它(记住我在几个地方使用 this.http.post
地方所以想在一个地方开始尝试)。我注意到使用 httpOptions
使 post
return Observable<ArrayBuffer>
。
疑问 1 - 为什么?
然后我把httpOptions
改成了
const httpOptions = {
headers: new HttpHeaders({ 'Content-Type': 'application/json' }),
withCredentials: true,
observe: 'response' as 'response',
responseType:'json'
};
它开始在我直接使用 http.post
的地方出现错误(没有 sendMessage
。错误是
ERROR in src/app/web-to-backend-interface.service.ts(81,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(112,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(196,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(216,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(251,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(291,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'.
所以我从 httpOptions
中删除了 responseType:'json'
并在我的 sendMessage
中的 this.http.post
中添加了一个显式对象!
private sendMessage(url:string, body:any,httpOptions){
...
let observable:Observable<HttpEvent<any>> = this.http.post(url,body,{
headers: new HttpHeaders({ 'Content-Type': 'application/json' }),
withCredentials: true,
observe: 'events',
responseType: 'json'
})
...
}
observe 和 responseType 之间似乎有关系。例如 observe=body 和 responseType=text 意味着 post 应该观察(查看)主体,将其解释为文本和 return 可观察。但我想知道为什么我就是不能改变 httpOptions
我有以下代码可以正常工作。
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 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
有几个重载方法,其中之一是 return Observable - angular.io/api/common/http/HttpClient#post。如何让 Angular 使用 returns 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'?
为什么为一个工作代码创建一个单独的函数会使其不可编译?我的错误是什么?
虽然我无法回答为什么你现在会收到 TS 错误,而你以前没有,但我可以建议你如何摆脱它(假设代码在你重构之前是有效的) .如果你在声明中给 response
一个类型,那么它不会得到你必须尝试映射的假定类型:
.pipe(tap((response: HttpEvent<any>) => {
let httpEvent = response; // No need for type-casting here anymore.
我能够解决它,但现在我的问题多于答案。如果有人能向我解释这种行为,我将不胜感激。
我正在使用以下 httpOptions
对象
const httpOptions = {
headers: new HttpHeaders({ 'Content-Type': 'application/json' }),
withCredentials: true,
observe: 'response' as 'response',
};
在代码中的某些地方按如下方式使用它。
return this.http.post(this.SIGNUP_USER_URL,body,httpOptions)
这有效。
然后我想到将 http.post
更改为函数 sendMessage
(如问题中所述)并仅在一次地方替换它(记住我在几个地方使用 this.http.post
地方所以想在一个地方开始尝试)。我注意到使用 httpOptions
使 post
return Observable<ArrayBuffer>
。
疑问 1 - 为什么?
然后我把httpOptions
改成了
const httpOptions = {
headers: new HttpHeaders({ 'Content-Type': 'application/json' }),
withCredentials: true,
observe: 'response' as 'response',
responseType:'json'
};
它开始在我直接使用 http.post
的地方出现错误(没有 sendMessage
。错误是
ERROR in src/app/web-to-backend-interface.service.ts(81,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(112,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(196,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(216,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(251,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(291,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'.
所以我从 httpOptions
中删除了 responseType:'json'
并在我的 sendMessage
中的 this.http.post
中添加了一个显式对象!
private sendMessage(url:string, body:any,httpOptions){
...
let observable:Observable<HttpEvent<any>> = this.http.post(url,body,{
headers: new HttpHeaders({ 'Content-Type': 'application/json' }),
withCredentials: true,
observe: 'events',
responseType: 'json'
})
...
}
observe 和 responseType 之间似乎有关系。例如 observe=body 和 responseType=text 意味着 post 应该观察(查看)主体,将其解释为文本和 return 可观察。但我想知道为什么我就是不能改变 httpOptions