猿问

可观察错误,尝试使用get方法

我正在尝试重构自定义 api get,如下所示:


 get( route: string, responseType: RespType = 'json', fullResponse: boolean = false, params = null): Observable<any> {

    return this.invoke( 'GET', route, null, responseType, fullResponse, true, params);

  }

对此:


@Injectable({

  providedIn: 'root'

})

export class DocumentCorrespondenceService {


  allCorrespondence: Observable<DossierEntry>;

  correspondenceEntries: Observable <DossierEntry>;

  attachmentEntries: Observable<DossierEntry>;




constructor(private http: HttpClient) { }



getDossierEntry( type: String = '' ): Observable<Array<DossierEntry>> {

  const entryType = type === '' ? 'all' : 'type/' + type;

  return this.http.get( '/api/patient/{patientUUID}/DossierEntry/' + entryType );

}


getDossierEntryFileData( entryID: number ): Observable<HttpResponse<Blob>> {

  return this.get( '/api/patient/{patientUUID}/DossierEntry/' + entryID + '/fileData', 'pdf', true );

}



}

但我现在收到这样的错误:


Type 'Observable<Object>' is not assignable to type 'Observable<DossierEntry[]>'.

  The 'Object' type is assignable to very few other types. Did you mean to use the 'any' type instead?

    Type 'Object' is missing the following properties from type 'DossierEntry[]': length, pop, push, concat, and 26 more.ts(2322)

那我必须改变什么?


眼眸繁星
浏览 194回答 1
1回答

有只小跳蛙

在新的 Angular 版本中(导致升级 typescript 版本),您必须指定 http 调用预期的类型:getDossierEntry( type: String = '' ): Observable<DossierEntry[]> {&nbsp; const entryType = type === '' ? 'all' : 'type/' + type;&nbsp; return this.http.get<DossierEntry[]>( '/api/patient/{patientUUID}/DossierEntry/' + entryType );}注意:我还用 改变了你的Array类型[],这更像是“javascript”
随时随地看视频慕课网APP

相关分类

JavaScript
我要回答