无法读取 Angular2 中未定义(…)的属性“push”

2023-11-30

这是错误的说法“无法读取未定义的属性‘push’”当使用下面的代码时

  let deal = new Deal(  5, 'afaf', 'dafa',5,23 ) 
  this.publicDeals.push( deal )   // gives a error saying Cannot read property 'push' of undefined(…)

整体如下图所示

export class Deal { 
  constructor(
    public id: number,
    public name: string,
    public  description: string,
    public originalPrice: number,
    public salePrice: number
      ) { }    
}

在另一个组件中,

import { Component, OnInit } from '@angular/core'; 
import { Deal } from '../deal';
import { DealService } from '../deal.service';

@Component({
  selector: 'public-deals',
  templateUrl: 'public-deals.component.html',
  styleUrls: ['public-deals.component.css']
})

export class PublicDealsComponent implements OnInit {
  publicDeals: Deal[];

  constructor(
    private dealService: DealService) {
  }

  ngOnInit(): void {    
      let deal = new Deal(  5, 'afaf', 'dafa',5,23 ) 
      this.publicDeals.push( deal )   // gives a error saying Cannot read property 'push' of undefined(…)

  }

  purchase(item){
    alert("You bought the: " + item.name);
  }
}

publicDeals未启动;

publicDeals: Deal[] = [];
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

无法读取 Angular2 中未定义(…)的属性“push” 的相关文章

随机推荐