【发布时间】:2016-02-16 08:32:59
【问题描述】:
我在 TS 中有简单的课程:
export class TestObj
{
public name:string;
public id:number;
public example:string;
}
简单的服务:
@Injectable()
export class SimpleService
{
private testObj:TestObj = new TestObj();
public constructor(@Inject(Http) private http:Http){}
public getTestObj():void
{
this.http.get("/rest/test2").map(res => res.json())
.do(data => console.log(<TestObj> data))
.catch(resp => Observable.throw(resp.json().error))
.subscribe(tobj => {
this.tobj = tobj;
console.log(this.tobj);
}, error => {log.error(error)})
}
如果我在 getTestObj() 之前在控制台日志 SimpleService.testObj 中打印,我看到了
<TestObj>
,但如果我运行 getTest,日志将是
<Object>
<Object>
是角虫吗?还是 Typescript 错误?
【问题讨论】:
-
顺便说一句,当您提供
http:Http类型时,TS 中不需要@Inject(Http)。
标签: angular typescript casting angular2-http