Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular 数组中的泛型类型?_Angular_Typescript_Ionic2 - Fatal编程技术网

Angular 数组中的泛型类型?

Angular 数组中的泛型类型?,angular,typescript,ionic2,Angular,Typescript,Ionic2,我试图创建一个模型应用程序,它以按钮列表的形式显示一些数据。我实现了一切,但我现在面临这个问题。我为数据初始化了一个数组和另一个类 -items.ts export class ItemPage { items: Item[] = []; constructor() { this.items.push( new Item(1, 'Apple', 'Green', 6, true)); this.items.push( new Item(

我试图创建一个模型应用程序,它以按钮列表的形式显示一些数据。我实现了一切,但我现在面临这个问题。我为数据初始化了一个数组和另一个类

-items.ts

  export class ItemPage {

  items: Item[] = [];

  constructor() {

    this.items.push(
      new Item(1, 'Apple', 'Green', 6, true));
    this.items.push(
      new Item(2, 'Banana', 'Yellow', 15, false));
  }
数据类如下所示

-item.ts

export class Item {
  constructor(
    id: number,
    fruit: string,
    color: string,
    quantity: number,
    onStock: boolean,
  ) { }
}
我在
items:Item[]=[]方面遇到了一个问题当我运行时
爱奥尼亚服务
。它说,
泛型类型“Item”需要两个类型参数
类型Item={[P in K]:T;}用一组类型为T的属性K构造一个类型

我试图解决这个问题,但没用,有人知道如何解决这个问题吗?


谢谢

我没有准确地再现这个问题,但最终得到了两个空对象。错误似乎是您没有将类中的属性设置为
public
,因此将类更改为:

导出类项目{
建造师(
公众id:号码,
公共水果:细绳,
公共颜色:字符串,
公共数量:数量,
公共onStock:boolean,
) { }
}
似乎工作正常:

但是如果您不需要在类中包含函数或其他原因,我强烈建议您应该使用接口。所以我想说你们制作界面:

导出接口项{
id:编号;
果:串;
颜色:字符串;
数量:数量;
onStock:布尔型;
}
构造函数(专用导航控制器:导航控制器){
this.items.push({id:1,水果:'Apple',颜色:'Green',数量:6,onStock:true});
this.items.push({id:2,水果:'Banana',颜色:'Yellow',数量:15,库存:false})
}

希望这有帮助

看起来,
是爱奥尼亚语中的保留字/类。尝试将您的
项目更改为其他名称。我尝试了您的建议,但我仍然有相同的问题Item[]也许?您可以将您对问题所做的代码更改添加到问题中吗?嘿,是的,它起到了作用,谢谢。如果我想在另一个班级里使用军队,我应该怎么做?我试过这样做:
export-class-Stats{public-stat:number;constructor(items:Item[]){this.stat=items.length;}}
但是它一直给我同样的错误
Generic-type'Item'需要2个类型参数
既然这样解决了你的问题,你能接受答案吗:)但是回到你的问题。。。什么是陆军,这是什么类
Stats
是一个新类还是什么?我真的建议你使用接口,但你又忘了在构造函数中设置
public
。。。我想你的类应该是这样的:
export-class-Stats{public-stat:number;constructor(public-stat:number,public-items:Item[]=[]){this.stat=items.length;}}
:)对不起,我的意思是数组,我的错。是的,这是名为
stats.ts
的新文件中的一个新类,我想在新类中使用
items.ts
中的数组
items:Item[]=[]
。我真的不确定您对items.ts中的数组项的确切含义。下面是一个与项一起实现新类的示例。。。