TypeError:_this.Order.orderRow未定义

时间:2018-02-02 17:01:54

标签: angular typescript

我正在上课,我遇到了一些麻烦:

我得到的信息是:

TypeError:_this.Order.orderRow未定义

当我尝试将新行推送到orderRow数组时会生成异常。

这是代码:

订单类

export class Order {

    public orderRow?: OrderRow[];
    public rowNumber?: number;

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

}

export class OrderRow {
    public rowNumber:number;
    public idSaleOrderRow?: number;
    public idCollection?: number;
    public idProduct?: number;
    public txtCollection?: string;
    public txtName?: string;
    public productName?: string;
    public vQty?: number;
    public vQtyFill?: number;
    public vSellPrice?: number;
    public vCustomerPrice?: number;
    public vCustomerPriceFinal?: number;

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

}

不能正常工作的代码

Order: Order = <Order>{};
...............
...............
let r:OrderRow = {
              rowNumber: newRowNumber,
              idSaleOrderRow: null,
              idCollection: res.result.idCollection,
              idProduct: res.result.idProduct,
              txtCollection: res.result.txtCollection,
              txtName: res.result.txtName,
              productName: res.result.productName,
              vQty: 1,
              vQtyFill: null,
              vSellPrice: res.result.vSellPrice,
              vCustomerPrice: res.result.vSellPrice,
              vCustomerPriceFinal: res.result.vSellPrice
            };

            this.Order.orderRow.push(r);

我该如何解决?

感谢支持

1 个答案:

答案 0 :(得分:2)

您难以将对象文字转换为构造Order类的新实例,您需要使用Order运算符调用new构造函数:

Order: Order = new Order({});

还应初始化orderRow字段:

export class Order {    
    public orderRow?: OrderRow[] = [];
}