简体   繁体   中英

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:

Argument of type '{ headers: HttpHeaders; responseType: string; }' is not assignable to parameter of type '{ headers?: HttpHeaders | { [header: string]: string | string[]; }; observe?: "body"; params?: HttpParams | { [param: string]: string | string[]; }; reportProgress?: boolean; responseType?: "json"; withCredentials?: boolean; }'. Types of property 'responseType' are incompatible. Type 'string' is not assignable to type '"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.

@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
    );
  }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM