为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

眼眸繁星 2021-10-21 17:18:31
我正在尝试重构自定义 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)那我必须改变什么?
查看完整描述

1 回答

?
有只小跳蛙

TA贡献1824条经验 获得超8个赞

在新的 Angular 版本中(导致升级 typescript 版本),您必须指定 http 调用预期的类型:


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

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

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

}

注意:我还用 改变了你的Array类型[],这更像是“javascript”


查看完整回答
反对 回复 2021-10-21
  • 1 回答
  • 0 关注
  • 193 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信