简体   繁体   English

Angular2 / TypeScript-类的类型数组的访问对象元素

[英]Angular2/TypeScript - Access object element of type array of class

I have this class that implements single properties and arrays of classes: 我有一个实现单个属性和类数组的类:

offert class 提供类

import {OffertRegion} from './offert-region';
import { OffertProduct } from './offert-product';

export class Offert {
    public idOffert: number;
    public OffertCode: string;


    public Regions: OffertRegion[];

    public Products: OffertProduct[];

constructor(values: Object = {}) {
        Object.assign(this, values);
    }

}

OffertRegion class OffertRegion类

export class OffertRegion {

    public idOffert: number;
    public idOffertRegion: number;
    public Region: string;
    public intOrder: number;

    constructor(values: Object = {}) {
        Object.assign(this, values);
    }
}

OffertProduct class Offert产品类别

export class OffertProduct {

    public idOffert: number;
    public OffertCode: string;
    public idOffertRegion: number;
    public Region: string;
    public intRegionOrder: number;
    public idOffertRow: number;
    public idProduct: number;
    public ProductCode: string;
    public Product: string;
    public Seats: number;
    public ImagePath: string;
    public Qty: number;


    constructor(values: Object = {}) {
        Object.assign(this, values);
    }
}

I don't know why, when I load the page I get this compilation error in the console: 我不知道为什么,当我加载页面时,在控制台中出现此编译错误:

offert-detail.component.ts (55,21): Property 'Products' does not exist on type 'Offert'. offert-detail.component.ts(55,21):类型“ Offert”上不存在属性“ Products”。

The piece of code that generates the error is this: 产生错误的代码是这样的:

s.subscribe(
      res => {
        console.log('header')
        console.log(res.header);
        console.log('regions')
        console.log(res.regions);
        console.log('products')
        console.log(res.products);

        this.offert = res.header;              
        this.offert.Regions = res.regions; //this is line 53

        this.offert.Products = res.products; // this is line 55!!!
      },
      error => this.errorMessage = <any>error
    );

Line 53 and line 55 are similar... Why the first works and the second one no? 53行和55行相似...为什么第一个可行,第二个不可行?

Even If I try just this two easy line I get the error: 即使我只尝试这两条简单的线,也会出现错误:

var rrr: OffertRegion[];  //OK
this.offert.Regions = rrr; //OK

var ooo: OffertProduct[];  //OK
this.offert.Products = ooo; //ERROR !!!

Thanks 谢谢

Finally I solved the problem... 终于我解决了这个问题...

I just stopped the server and run ng serve again... 我刚刚停止服务器并再次运行ng serve ...

Now it works! 现在可以了!

Probably I have make a mess with copy / paste / rename files... Ciao! 可能我把复制/粘贴/重命名文件弄得一团糟... Ciao!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM