У меня проблема со значением запроса Http. Я делаю Http-запрос к отдыху Express API, и я хотел бы видеть значение во всем компоненте. Я могу иметь данные о наблюдаемой, но не в другой функции моего компонента. Не могли бы вы объяснить, почему?
import { Component, OnInit, Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
export class UserModel {
constructor (
public id: string,
public name: string,
public type: string
) {}
}
@Injectable()
export class AskingService {
BASE_URL = 'http://localhost:4201/';
constructor(private http: HttpClient) { }
// Get users from EXPRESS API REST
getUserFromBdd() {
return this.http.get<UserModel[]>(this.BASE_URL + 'api/fields');
}
}
@Component({
selector: 'app-asking-problem',
template: `
`
})
export class AskingProblemComponent implements OnInit {
constructor( private service: AskingService) { }
users;
ngOnInit() {
// subscribing to the service
this.service.getUserFromBdd().subscribe(data => {this.users = data, console.log('this.users =', this.users); });
// console return data
console.log('this.users =', this.users);
// console return undefined
}
}