首页 文章

TypeError:无法读取undefined-IONIC2的属性'push'

提问于
浏览
0

我正在尝试 Push 迭代值为 array

json_resp 是Json的回应 .

我的打字稿代码

export class hello 
{

CategoryLst:any[];    //array

var catIndex,BillerIndex;

for(catIndex= 0; catIndex<= json_resp.category.length; catIndex++) {
 var Clst=json_resp.category[0].categoryName;

this.CategoryLst.push(Clst);

}
}

在尝试执行其抛出错误时

ORIGINAL EXCEPTION: TypeError: Cannot read property 'push' of undefined

有什么我错过了吗?

2 回答

  • 3

    CategoryLst:any[] 只是指定 array 的类型,但没有指定它,因此默认情况下该数组的值为 undefined .

    为了在同一个声明中初始化它,你应该这样做:

    CategoryLst:any[] = [];
    
  • 3

    试试这个

    CategoryLst:any[] = [];
    

相关问题