首页 > 解决方案 > Angular TypeScript:无法将数组分配给新数组

问题描述

所以在这个组件中,我从 api“adherents”中获取了一个对象数组,它工作正常,我用来显示一个表,但是当我创建一个新数组“temp_adherents”,并将第一个数组分配给它并尝试使用它,它是空的,在控制台中,我收到一个新数组未定义的错误,这是我的代码:


export class AdherentsComponent implements OnInit {
  adherents: adherent[];

  constructor(private adherentService: AdherentService, private alertify: AlertifyService) { }

  ngOnInit() {
    this.getAdherents();
  }

  temp_adherents = this.adherents // this is where the problem is

  getAdherents() {
    this.adherentService.getAdherents().subscribe((
      adherents: adherent[]) => {
      this.adherents = adherents;
    }, error => { this.alertify.error(error); })
  }


}


标签: javascriptarraysangularjstypescript

解决方案


推荐阅读