我正在尝试使用http
在Angular
中打印rxjs
调用的结果
考虑以下代码
import { Component, Injectable, OnInit } from '@angular/core';
import { Http, HTTP_PROVIDERS } from '@angular/http';
import 'rxjs/Rx';
@Injectable()
class myHTTPService {
constructor(private http: Http) {}
configEndPoint: string = '/my_url/get_config';
getConfig() {
return this.http
.get(this.configEndPoint)
.map(res => res.json());
}
}
@Component({
selector: 'my-app',
templateUrl: './myTemplate',
providers: [HTTP_PROVIDERS, myHTTPService],
})
export class AppComponent implements OnInit {
constructor(private myService: myHTTPService) { }
ngOnInit() {
console.log(this.myService.getConfig());
}
}
每当我尝试打印
getconfig
的结果时,它总是返回Observable {_isScalar: false, source: Observable, operator: MapOperator}
即使我返回一个json对象。
如何打印
getConfig
的结果? 最佳答案
您需要订阅可观察对象,并传递一个处理发出的值的回调
this.myService.getConfig().subscribe(val => console.log(val));