не может получить значения используя any = {} в машинописном наборе angular8 - PullRequest
0 голосов
/ 19 апреля 2020
import { Component, OnInit  } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { enableProdMode } from '@angular/core';
enableProdMode();

@Component({
  selector: 'app-home',
  templateUrl: './home.component.html',
  styleUrls: ['./home.component.css']
})
export class HomeComponent implements OnInit {
   values: any = {};


  constructor(private http: HttpClient) { }

  ngOnInit() {
    this.getValues();
    console.log(this.values);
  }

   getValues()
   {
     this.http.get('http://localhost:5000/api/values').subscribe(Response => { this.values = Response},error => {console.log(error); 
     });
   }

}

Я могу получить значения из ответа HTTP. Но я не могу сохранить эти значения в объекте 'values'. получить пустой массив.

1 Ответ

0 голосов
/ 19 апреля 2020

Вам нужно поместить console.log в подписку, так как он вызывается асинхронно к моменту запуска вашего console.log, результата нет.

getValues()
{
     this.http.get('http://localhost:5000/api/values').subscribe(Response => { 
        this.values = Response;
        console.log("##Got values", this.values);
        },error => {console.log(error);      
   });
}
...