我在 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 '{}'.
我遵循了 the angular 2 documentation 中的示例,并且有效。我只是厌倦了盯着这个错误。我错过了什么吗?
最佳答案
对我来说这看起来很奇怪......
.map(res => (<Response>res).json())
我愿意
.map((res: Response) => res.json())
关于angular - 类型 'json' 上不存在属性 '{}',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33919710/