[英]Property 'json' does not exist on type '{}'
我在Typescript中有一个抽象基类,如下所示:
import {Http, Headers, Response} from 'angular2/http';
export abstract class SomeService {
constructor(private http:Http) {}
protected post(path:string, data:Object) {
let stringifiedData = JSON.stringify(data);
let headers = new Headers();
headers.append('Content-Type', 'application/json');
headers.append('Accept', 'application/json');
this.http.post(`http://api.example.com/${path}`, stringifiedData, { headers })
.map(res => res.json())
.subscribe(obj => console.log(obj));
}
}
它完美地运作。 但是,Typescript编译器抱怨.map(res => res.json())
。 我一直收到这个错误:
ERROR in ./src/app/components/shared/something/some.abstract.service.ts
(13,29): error TS2339: Property 'json' does not exist on type '{}'.
我按照角度2文档中的示例进行操作, 它可以工作 。 我只是厌倦了盯着这个错误。 我错过了什么吗?
对我来说这看起来很奇怪......
.map(res => (<Response>res).json())
我会做
.map((res: Response) => res.json())
你可以通过类型断言来消除这个错误到Response
:
.map((res: Response) => res.json())
http.post()
将返回一个Observable<Response>
上至极map
将需要类型的对象Response
。 我认为这是当前TypeScript AngularJS .d.ts
缺少的定义。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.