簡體   English   中英

Angular 將數組分配給另一個數組時出現未定義錯誤

[英]Angular undefined error when assigning array to another array

所以在這個組件中,我從父組件獲取了一個對象數組,我用它來顯示一個對象列表,但是當我創建一個新數組並將第一個數組分配給它並嘗試使用它時,我收到新數組未定義的錯誤,這是我的代碼:

export class AdherentsListComponent implements OnInit {
  // adherents: Adherent[];
  adherent: Adherent;

  @Input() adherents: Adherent[];

 
  constructor(private adherentService: AdherentService, private alertify: AlertifyService, private router: Router,private modalService: NgbModal) { }

  ngOnInit() {
  }
  
   tempAdherents = this.adherents; //this is what's not working

   loadAdherent(id){
    this.adherentService.getAdherent(id).subscribe((adherent: Adherent) => {
      this.adherent = adherent;
    }, error => {
      this.alertify.error(error);
    })
  }

}

請知道如何創建新數組,謝謝。

嘗試初始化您的輸入,例如 @Input() 追隨者: Adherent[] = [] as Adherent[];

您缺少tempAdherents的聲明。

export class AdherentsListComponent implements OnInit {
  // adherents: Adherent[];
  adherent: Adherent;

  @Input() adherents: Adherent[];

 
  constructor(private adherentService: AdherentService, private alertify: AlertifyService, private router: Router,private modalService: NgbModal) { }

  ngOnInit() {
  }
  
   var tempAdherents = this.adherents; //this is what's not working

   loadAdherent(id){
    this.adherentService.getAdherent(id).subscribe((adherent: Adherent) => {
      this.adherent = adherent;
    }, error => {
      this.alertify.error(error);
    })
  }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM