更新
我注意到一种奇怪的行为!我在这个服务中有几个方法,我得到不同的响应(一个映射正确,另一个看起来很奇怪)。我更新了代码。
someService.ts:
import {Http, Response} from '@angular/http';
import {Observable} from 'rxjs/Observable';
import 'rxjs/add/operator/map';
@Injectable()
export class someService {
constructor(private _http:Http) { }
getSession():Observable<IUser> { // i get a response like that [object object]
this.getsession = this._http.get("/url");
return this.getsession.map((response) => <IUser> response.json());
}
getUser():Observable<IUser> { // i get the the response object mapped here as one of the comments said the response doesn't have data object
this.user = this._http.get("/url");
return this.user.map(response => <User> response.json());
}
}
user.component.ts
import { Component, OnInit } from '@angular/core';
import {IUser} from "../common/user";
import {SomeService} from "../services/someService.service";
@Component({
selector: "user",
templateUrl: "./user.component.html",
})
export class User implements OnInit {
user:IUser;
constructor( private _someService:SomeService) {
}
getUser() {
this._someService.getSession().subscribe((data) => {
this.user = data; // the response looks like this: [object Object]
}, (err) => {
this.errorMsg = ""
});
}
ngOnInit():void {
}
}
我从休息中得到的JSON对象:
{"uid":"***","name":"Test","mail":null,"gname":null,"sn":null,"isEn":true,"user":"**"}
答案 0 :(得分:1)
尝试在顶部添加此导入
如果您想要所有运营商
import 'rxjs/Rx';
如果您只想要地图运算符
import 'rxjs/add/operator/map';
如果两者都不起作用,请尝试重新启动IDE