【发布时间】:2022-01-03 15:58:39
【问题描述】:
我正在开发一个 Angular 应用程序。在我的组件.ts 文件中,我有一个数组如下:
public myArray = [];
public dataFromAPI = [];
每当我尝试在此数组中推送一个元素时,在我的一种方法中,它都会给我以下错误:
this.appService.getData.subscribe(resp => {
if (resp != null) {
this.dataFromAPI = resp;
this.dataFromAPI.forEach(element => {
this.myArray.push(element)
})
}
RROR TypeError: Cannot read properties of null (reading 'push')
我的代码中没有发现任何错误。我总是像这样定义一个新数组,但我第一次遇到这个错误。如何解决?
【问题讨论】:
-
您好,请问您可以将
push元素所在的代码添加到此特定数组吗? -
@Roma,能否请您在 forEach 循环中创建
this的 console.log,以检查 forEach 中this的值是什么
标签: javascript angular typescript angular9