简体   繁体   English

“标记”类型上不存在属性“_popup”<any> '</any>

[英]Property '_popup' does not exist on type 'Marker<any>'

So, I am building a map using angular and leaflet.因此,我正在使用 angular 和 leaflet 构建 map。 One of the things that I use is leaflet.markercluster.我使用的其中一件事是 leaflet.markercluster。 When i click on the cluster I want the popup content of a random marker of a cluster to be written somewhere.当我单击集群时,我希望将集群的随机标记的弹出内容写入某处。 To access the popup content of some random cluster I did this:要访问一些随机集群的弹出内容,我这样做了:

 cluster.getAllChildMarkers()[0]._popup._content

and got an error: Property '_popup' does not exist on type 'Marker'.并得到一个错误: “标记”类型上不存在属性“_popup”。

But the thing is, if I do ng serve first time it failes to compile, but if I change anything and save all it compiles sucessfully with the errors and I can see the content of the popup.但问题是,如果我第一次执行服务,它无法编译,但如果我更改任何内容并保存所有内容,它会成功编译并出现错误,我可以看到弹出窗口的内容。

Also, if I do console.log(cluster.getAllChildMarkers()[0]) and I inspect element on webpage I get the regular console log of a marker with latlng andall other atributtes, including _popup.此外,如果我执行console.log(cluster.getAllChildMarkers()[0])并检查网页上的元素,我会得到带有 latlng 和所有其他属性(包括 _popup)的标记的常规控制台日志。

Does anybody know why does typescript/vscode log an error, but html console sees it normally?有人知道为什么 typescript/vscode 会记录错误,但 html 控制台可以正常看到吗?

Because TypeScript is more strict than JavaScript, it warns you of potential issues that may actually work just fine once transpiled in JS.因为 TypeScript 比 JavaScript 更严格,所以它会警告您潜在的问题,一旦在 JS 中转译,这些问题实际上就可以正常工作。

In this specific case, this is simply due to the pseudo private properties ("_popup" follows the usual JS libraries convention of using an underscore _ prefix to denote pseudo private members) not being declared on the TS types of Leaflet, since you are not expected to use them.在这种特定情况下,这仅仅是由于 Leaflet 的 TS 类型上未声明伪私有属性(“_popup”遵循使用下划线 _ 前缀表示伪私有成员的常用 JS 库约定),因为您不是预计会使用它们。

But of course this is still technically valid in JS, so you can tell the TS compiler "I know what I am doing" by using the //@ts-ignore comment directive just above that line.但当然,这在 JS 中在技术上仍然有效,因此您可以使用该行上方的//@ts-ignore注释指令告诉 TS 编译器“我知道我在做什么”。

Or longer but much better, since you can remain under TS watch: use actual Leaflet API to achieve what you are doing:或者更长但更好,因为你可以保持在 TS 监视下:使用实际的 Leaflet API 来实现你正在做的事情:

cluster.getAllChildMarkers()[0].getPopup()?.getContent()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM