나는 angular2에 익숙하지 만, 도움이 필요합니다. 아래 서비스 클래스를 사용 중입니다.angular2 observable, 구성 요소에서 정의되지 않음
import {Injectable} from 'angular2/core';
import {Http} from 'angular2/http';
import 'rxjs/add/operator/map';
@Injectable()
export class UsersService {
private _url = "http://jsonplaceholder.typicode.com/users";
constructor(private _http: Http){
}
getUsers(){
return this._http.get(this._url)
.map(res => res.json());
}
}
아래 서비스에서 위의 서비스를 호출 할 때 정의되지 않은 값이 표시됩니다.
import{Component, OnInit} from 'angular2/core';
import{UsersService} from './users.service';
@Component({
selector: 'users',
template:'In users',
//templateUrl: 'app/users.component.html',
providers: [UsersService]
})
export class UsersComponent implements OnInit{
users: any[];
constructor(private _service: UsersService){
}
ngOnInit(){
this._service.getUsers()
.subscribe(result => this.users = result);
console.log(this.users);
}
}
그러나 서비스 클래스의 콘솔에 값을 기록하려고하면 해당 서비스가 표시됩니다. 어떤 도움이라도 매우 높을 것입니다. 감사합니다.
감사 @Alexis, 그것은 많은 도움이되었습니다. – user6762225