【问题标题】:Type 'Observable<UI[] | Promise<any>>' is not assignable to type 'Observable<UI[]>'. Type 'UI[] | Promise<any>' is not assignable to type 'UI[]'输入'可观察<UI[] | Promise<any>>' 不可分配给类型 'Observable<UI[]>'。键入'UI [] | Promise<any>' 不可分配给类型 'UI[]'
【发布时间】:2020-07-21 21:57:31
【问题描述】:

我需要返回一个 UI 列表,我执行此方法:

getList(): Observable<UI[]> { 
        return this.httpClient.get("/api/listui").pipe(map(
            (response:Response) => {  return response.json(); },
        ),
            catchError((error) => { return throwError(error); }));
    }

在我的 vs 代码中,错误显示为:

Type 'Observable&lt;UI[] | Promise&lt;any&gt;&gt;' is not assignable to type 'Observable&lt;UI[]&gt;'. Type 'UI[] | Promise&lt;any&gt;' is not assignable to type 'UI[]'。”

谁能帮忙解决这个问题?

【问题讨论】:

    标签: angular angular9 angular-observable


    【解决方案1】:

    httpClient默认会返回json数据你不需要这样做return response.json();

    getList(): Observable<UI[]> { 
            return this.httpClient.get("/api/listui").pipe(
                catchError((error) => { return throwError(error); }));
        }
    
    

    【讨论】:

      猜你喜欢
      • 2019-04-22
      • 2018-04-23
      • 2021-12-10
      • 2017-08-12
      • 2018-08-30
      • 2018-08-14
      • 1970-01-01
      • 2021-09-23
      • 2023-04-10
      相关资源
      最近更新 更多