本文介绍了属性“JSON”的类型不存在“{}”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在打字稿的抽象基类,看起来像这样:
I have an abstract base class in Typescript that looks like this:
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(`api.example/${path}`, stringifiedData, { headers }) .map(res => res.json()) .subscribe(obj => console.log(obj)); } }它完美。然而,打字稿编译器抱怨 .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文档和它的工作原理的。我只是在生病这个错误盯着。我缺少的东西吗?
I followed the examples in the angular 2 documentation, and it works. I'm just sick of staring at this error. Am I missing something?
推荐答案您可以摆脱这个错误被类型断言为响应:
You can get rid of this error by type-assertion to Response:
.map(res => (<Response>res).json())http.post()将返回观测与LT;响应&GT; 上至极地图将需要类型的对象响应。我认为这是在当前的打字稿AngularJS缺少定义 .d.ts 。
http.post() will return a Observable<Response> on wich map will require an Object of type Response. I think that's a missing definition in the current TypeScript AngularJS .d.ts.
更多推荐
属性“JSON”的类型不存在“{}”
发布评论