Angular TypeScript将项目推送到数组错误

时间:2017-12-09 15:53:04

标签: angular typescript

export class PickGroupService {
  channelList: any[];
  pick5West = ['783', '679', '934', '946', '800'];
  thePicks: any[];

  constructor(private channels: ChannelsService) { this.channelList = this.channels.getChannels(); }

  getPickSet(pick, region) {
    if (pick === 'pick5') {
      switch (region) {
        case 'west':
        for (let i = 0; i < this.pick5West.length; i++) {
          this.thePicks.push((this.channelList.filter(x => x.pickCode === this.pick5West[i]));
        }
        console.log(this.thePicks);
  }
}

}  } 只是想知道这段代码有什么问题,它说: ERROR 在浏览器控制台上输入TypeError:this_1.thePicks未定义。请指教!

1 个答案:

答案 0 :(得分:4)

将其初始化为空数组

 thePicks: any[] = [];