Делал все по инструкциям с angular.io
app.service.ts:
import { Injectable } from '@angular/core';
import { Http, Response } from '@angular/http';
import { Observable } from 'rxjs/Observable';
@Injectable()
export class AppService {
constructor (private http: Http) {}
private dataUrl = 'http://jsonplaceholder.typicode.com/users';
private extractData(res: Response) {
let body = res.json();
return body || {};
}
getData (): Observable<any> {
return this.http.get(this.dataUrl)
.map(this.extractData)
}
}
app.component.ts:
import { Component, OnInit } from '@angular/core';
import { AppService } from './app.service';
import '../../public/css/styles.css';
@Component({
selector: 'my-app',
providers: [AppService],
template: require('./app.component.html'),
styles: [require('./app.component.css')],
})
export class AppComponent implements OnInit {
private users:any;
constructor(private appService: AppService) {}
ngOnInit() {
this.appService.getData().subscribe(res => this.users = res);
console.log(this.users);
}
}
Получаю в консоле undefined