类型"{}"上不存在属性"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) => {