【发布时间】:2018-04-24 09:37:18
【问题描述】:
我尝试使用 http.get 从后端获取一个数组。作为回应,我有:["Item1","Item2","Item3"]。
constructor(private http:Http){
this.http.get('api').subscribe(
data => {this.array = data}
);
}
上面的代码使 this.array = undefined。 我应该使用什么来获取数组?
【问题讨论】:
-
你在哪里访问
this.array? -
在构造函数中,在http.get之后
-
@user852 你检查我的答案了吗?
-
你不能把
console.log(this.array)放在http.get之后,因为它是async。你必须把它放在订阅里面