类型"{}"上不存在属性"xxx" [英] Property 'xxx' does not exist on type '{}'

查看:150
本文介绍了类型"{}"上不存在属性"xxx"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我今天在我的角度应用程序中遇到了一种奇怪的行为.

I run into a strange behavior in my angular app today.

ERROR in src/main(A,B): error TS2339: Property 'XXX' does not exist on type '{}'.

错误发生在下面的代码行

The error occured at the following line of code

xxx.subscribe(data => {
    this.var = data.XXX
});

因此,基本上,我订阅了一个可观察对象,并将数据从可观察对象映射到全局变量.我的角度应用程序按预期工作,因此此错误似乎对应用程序本身没有影响,但我仍然无法弄清这里出了什么问题.以下是data的控制台日志:

So basically i subscribe to an observable and map the data from the observable to an global var. My angular App works as intended, so this error seems to have no effect on the app itself, but I still can't figure out whats wrong here. Below is the console log of data:

console.log(data)
//{"XXX": "test"}

推荐答案

Typescript抱怨您在数据中没有该属性,您可以使用任何属性或使用该属性创建一个接口,

Typescript complains that you do not have the property inside the data, you can either use any or create an inteface with that property,

xxx.subscribe((data:any) => {

这篇关于类型"{}"上不存在属性"xxx"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆