类型 'IProductsAvailable' 缺少类型"IProductsAvailable[]"中的以下属性:长度、弹出、推送、连接等 28 个属性



在尝试使用应用程序中的接口时,有人能帮我解决以下错误吗

Type 'IProductsAvailable' is missing the following properties from type 'IProductsAvailable[]': length, pop, push, concat, and 28 more

服务

getProducts() {
return this.http.get<IProductsAvailable>("https://fakestoreapi.com/products")
}

接口

export interface IProductsAvailable {
id: string;
title: string;
category: string;
description: number;
image: string;
price: number;
rating: []
}  

组件

productsAvailable:IProductsAvailable[];
this._productsService.getProducts().subscribe((res)=> {
console.log(res);
this.productsAvailable = res;
});

当我试图将来自服务的响应分配给productsAvailable变量时,我得到了错误。

在您的服务中,您应该指定泛型类型应该是数组。

getProducts() {
return this.http.get<IProductsAvailable[]>("https://fakestoreapi.com/products")
}

将服务更改为:

getProducts(): Observable<Array<IProductsAvailable>> {
return this.http.get<Array<IProductsAvailable>>
("https://fakestoreapi.com/products");
}

最新更新