属性“JSON”的类型不存在“{}”

编程入门 行业动态 更新时间:2024-10-28 19:31:18
本文介绍了属性“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”的类型不存在“{}”

本文发布于:2023-11-15 18:57:15,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1597105.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:不存在   属性   类型   JSON

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!