Angular2/TypeScript - Access object element of type array of class

时间:2017-04-13 14:55:41

标签: angular typescript compiler-errors

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

export class OffertRegion {

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

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

OffertProduct class

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'.

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?

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

1 个答案:

答案 0 :(得分:0)

最后我解决了这个问题...

我刚刚停止服务器并再次运行 ng serve ...

现在有效!

可能我弄乱了复制/粘贴/重命名文件...... 侨!