尝试将数据加载到数组时数组未定义
Array is undefined when trying to load data into Array
我正在尝试将数据加载到数组中,只是添加了 soem console.log 来测试它是否有效。
reload(){
this.dataService.getPostoffice()
.subscribe(data => this.poArray= data);
console.log(this.poArray);
}
很遗憾,此时数组未定义。我的意思是我将数据填充到那里的数组中,但它仍然未定义。如果我稍后在代码中尝试 console.log 该数组,它可以正常工作并记录数组的完整数据。有人能帮我吗? :)
您正在异步填充数组,这就是填充数组但未在正确时间显示它的原因。
reload(){
this.dataService.getPostoffice()
.subscribe(data => {
this.poArray= data;
console.log(this.poArray);
});
}
在这里,您需要像下面的代码片段一样在订阅中安装控制台:
reload(){
this.dataService.getPostoffice()
.subscribe(data => {
this.poArray= data;
console.log(this.poArray);
});
}
希望对您有所帮助。
我正在尝试将数据加载到数组中,只是添加了 soem console.log 来测试它是否有效。
reload(){
this.dataService.getPostoffice()
.subscribe(data => this.poArray= data);
console.log(this.poArray);
}
很遗憾,此时数组未定义。我的意思是我将数据填充到那里的数组中,但它仍然未定义。如果我稍后在代码中尝试 console.log 该数组,它可以正常工作并记录数组的完整数据。有人能帮我吗? :)
您正在异步填充数组,这就是填充数组但未在正确时间显示它的原因。
reload(){
this.dataService.getPostoffice()
.subscribe(data => {
this.poArray= data;
console.log(this.poArray);
});
}
在这里,您需要像下面的代码片段一样在订阅中安装控制台:
reload(){
this.dataService.getPostoffice()
.subscribe(data => {
this.poArray= data;
console.log(this.poArray);
});
}
希望对您有所帮助。