我想在http请求和订阅之后保存数据,但我无法在第一个索引处看到数据。它在浏览器中看起来像这样。使用this.mySet [0]我无法访问此内容。怎么了?
[] 0:" test" length:1__proto__:Array(0)
api.service.ts:409
myComponent.ts
mySet = [];
getLageSub() {
this.apiService.getLage().subscribe(data => {this.mySet.push(data)});
console.log(this.mySet)<------here is my output but I will need it elsewhere
}
myService.ts
getLage() {
let url = 'rest/getData-pool';
try {
return this.http.post(url, {}, this.getPutHeaders()).map((res) =>res.json())
} catch (error) {
console.log(error);
}
}
答案 0 :(得分:0)
试试这个案例:
mySet:any;构造函数(){this.mySet = [];}
getLageSub(){this.apiService.getLage()。subscribe(data =&gt; {this.mySet.push(data)}); console.log(this.mySet)&lt; ------这是我的输出,但我需要它在其他地方}
答案 1 :(得分:0)
我希望它会有所帮助:
mySet:any;构造函数(){this.mySet = [];}
getLageSub(){this.apiService.getLage()。subscribe(data =&gt; {this.mySet.push(JSON.parse(JSON.stringify(data)))}); console.log(this.mySet)&lt; ------这是我的输出,但我需要它在其他地方}