简体   繁体   English

订阅后将值保存到数组或变量中

[英]After subscribe save value into array or variable

I want to save the data after http request and subscribing like below but I cant see the data at the first index. 我想在http请求和下面的订阅之后保存数据,但是我看不到第一个索引的数据。 It looks like this within the browser. 在浏览器中看起来像这样。 With this.mySet[0] I have not access to this. 有了this.mySet [0],我无法访问它。 What it wrong? 怎么了

[]0: "test"length: 1__proto__: Array(0) [] 0:“测试”长度:1__proto __:Array(0)

api.service.ts:409 api.service.ts:409

myComponent.ts 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 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);
    }
  }

Try this case : 试试这种情况:

mySet : any;Constructor(){this.mySet = [];} mySet:任意; Constructor(){this.mySet = [];}

getLageSub() { this.apiService.getLage().subscribe(data => {this.mySet.push(data)}); getLageSub(){this.apiService.getLage()。subscribe(data => {this.mySet.push(data)}); console.log(this.mySet)<------here is my output but I will need it elsewhere } console.log(this.mySet)<------这是我的输出,但是我将在其他地方使用它}

I hope it will help: 希望对您有所帮助:

mySet : any;Constructor(){this.mySet = [];} mySet:任意; Constructor(){this.mySet = [];}

getLageSub() { this.apiService.getLage().subscribe(data => {this.mySet.push(JSON.parse(JSON.stringify(data)))}); getLageSub(){this.apiService.getLage()。subscribe(data => {this.mySet.push(JSON.parse(JSON.stringify(data)))});; console.log(this.mySet)<------here is my output but I will need it elsewhere } console.log(this.mySet)<------这是我的输出,但是我将在其他地方使用它}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM