当前位置: 首页 > 知识库问答 >
问题:

nativescript-类型为“Object”的tns build android参数的错误不能分配给类型为“Response”的参数

凤财
2023-03-14

我正在构建一个nativescript应用程序,并在tns构建Android时得到这些错误:

src/app/services/cabin.service.ts(21,86):错误TS2345:类型为“Object”的参数不能分配给类型为“Response”的参数。“Object”类型可分配给极少数其他类型。您的意思是使用“any”类型来代替吗?类型“Object”中缺少属性“type”。src/app/services/cabin.service.ts(27,86):错误TS2345:类型为“Object”的参数不能分配给类型为“Response”的参数。“Object”类型可分配给极少数其他类型。您的意思是使用“any”类型来代替吗?src/app/services/cabin.service.ts(33,86):错误TS2345:类型为“Object”的参数不能分配给类型为“Response”的参数。“Object”类型可分配给极少数其他类型。您的意思是使用“any”类型来代替吗?

这是我的橱柜

import { Injectable } from '@angular/core';
import { Cabin } from '../shared/cabin';
import { Observable } from 'rxjs';
import { Http, Response } from '@angular/http';
import { HttpClient, HttpHeaders } from "@angular/common/http";
import { baseURL } from '../shared/baseurl';
import { ProcessHTTPMsgService } from './process-httpmsg.service';
import { map } from "rxjs/operators";
import { catchError } from "rxjs/operators";

@Injectable()
export class CabinService {

  cabins: Cabin[];

  constructor(private http: HttpClient,
              private processHTTPMsgService: ProcessHTTPMsgService) { }

  getCabins(): Observable<Cabin[]> {
    return this.http.get(baseURL + 'cabins')
                    .pipe(map(res => { return this.processHTTPMsgService.extractData(res); }),
                    catchError(error => { return this.processHTTPMsgService.handleError(error); }));
  }

  getCabin(id: number): Observable<Cabin> {
    return  this.http.get(baseURL + 'cabins/'+ id)
                    .pipe(map(res => { return this.processHTTPMsgService.extractData(res); }),
                    catchError(error => { return this.processHTTPMsgService.handleError(error); }));
  }

  getFeaturedCabin(): Observable<Cabin> {
    return this.http.get(baseURL + 'cabins?featured=true')
                    .pipe(map(res => { return this.processHTTPMsgService.extractData(res)[0]; }),
                    catchError(error => { return this.processHTTPMsgService.handleError(error); }));
  }

}

第21、23、33行的代码如下:

 .pipe(map(res => { return this.processHTTPMsgService.extractData(res); }),

我对打字机和原生机是新手

这是我的ProcessHttpMessage.Service从“@Angular/Core”导入{Injectable};

import { Observable } from 'rxjs';
import { Http, Response } from '@angular/http';
import { throwError } from 'rxjs';

@Injectable()
export class ProcessHTTPMsgService {

  constructor() { }

  public extractData(res: Response) {
    let body = res.json();
    return body || { };
  }

  public handleError (error: Response | any) {
    let errMsg: string;
    if (error instanceof Response) {
      const body = error.json() || '';
      const err = body.error || JSON.stringify(body);
      errMsg = `${error.status} - ${error.statusText || ''} ${err}`;
    } else {
      errMsg = error.message ? error.message : error.toString();
    }
    return throwError(errMsg);


  }
}

共有1个答案

漆雕奇
2023-03-14

在使用HttpClient时,默认情况下将接收JSON数据,除非修改responseType或observe参数。

res已经是您的JSON对象,因此它应该是

public extractData(res: any) {
    // Unless you have further extraction to be done, this method is not required at all
    return res;
}
 类似资料: