[英]Getting typescript error for options configuration in an Angular 8 app
I'm getting the following error when trying to call a service using rest api and a get call: 尝试使用rest api和get调用服务时出现以下错误:
Argument of type '{ headers: HttpHeaders;
类型'{标题的参数:HttpHeaders; responseType: string;
responseType:字符串; }' is not assignable to parameter of type '{ headers?: HttpHeaders |
}'不可分配给类型'{headers ?: HttpHeaders | { [header: string]: string |
{[header:string]:字符串| string[];
串[]; };
}; observe?: "body";
观察? params?: HttpParams |
参数?:HttpParams | { [param: string]: string |
{[参数:字符串]:字符串| string[];
串[]; };
}; reportProgress?: boolean;
reportProgress ?:布尔值; responseType?: "json";
responseType ?:“ json”; withCredentials?: boolean;
withCredentials ?:布尔值; }'.
}'。 Types of property 'responseType' are incompatible.
属性“ responseType”的类型不兼容。 Type 'string' is not assignable to type '"json"'.ts(2345)
类型“字符串”不可分配给类型““ json””。ts(2345)
const httpOptionsText = {
headers: new HttpHeaders({
'Accept': 'text/plain',
'Content-Type': 'text/plain'
}),
responseType: 'text'
};
Here is the service call with httpOptionsPlain as parameter which is error is signed. 这是带有httpOptionsPlain作为参数的服务调用,该服务调用已签名错误。
@Injectable()
export class TripService {
public getTripNameById(tripId: number): Observable<any> {
return this.http.get<any>(`${this.baseUrl}/Trips/Trip/Name/${tripId}`, httpOptionsText);
}
The error is on the editor only (the code works fine). 该错误仅在编辑器上(代码可以正常工作)。 Thanks.
谢谢。
You should change your code to 您应该将代码更改为
public getTripNameById(tripId: number): Observable<any> {
const httpOptionsText = {
headers: new HttpHeaders({
Accept: "text/plain",
"Content-Type": "text/plain"
}),
responseType: "text" as "json"
};
return this.http.get<any>(
`${this.baseUrl}/Trips/Trip/Name/${tripId}`,
httpOptionsText
);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.