angular - ES6:“对象”类型上不存在“选定”属性
问题描述
从“AMcharts4 codepen 预选区域”,我想将 JS 转换为 ES6。但是,我得到了错误
错误 TS2339:“对象”类型上不存在“已选择”属性。
我要转换的代码如下:
// Create map instance
let chart = am4core.create("chartdivmap", am4maps.MapChart);
// Set map definition
chart.geodata = am4geodata_worldHigh;
// Set projection
chart.projection = new am4maps.projections.Miller();
// Center on the groups by default
chart.homeZoomLevel = 1.5;
chart.homeGeoPoint = {
longitude: 10,
latitude: 52
};
// Polygon series
let polygonSeries = chart.series.push(new am4maps.MapPolygonSeries());
polygonSeries.useGeodata = true;
var polygonTemplate = polygonSeries.mapPolygons.template;
polygonTemplate.tooltipText = "{name}";
polygonTemplate.fill = chart.colors.getIndex(0);
// Hover state
var hs = polygonTemplate.states.create("hover");
polygonTemplate.fill = am4core.color("#CCCCCC");
hs.properties.fill = am4core.color("#010101");
polygonTemplate.adapter.add("fill", function(fill, target) {
if (target.dataItem.dataContext && target.dataItem.dataContext.selected) {
return am4core.color("#666666");
}
return fill;
});
我尝试通过let k:any = target;
并传递变量 likefunction(fill, target, k)
并尝试捕获类似的值:k.dataItem.dataContext.selected
这给了我更多错误。
解决方案
你可以尝试这样的事情:
polygonTemplate.adapter.add("fill", function(fill, target) {
const ctx = target.dataItem.dataContext as any;
if (ctx && ctx.selected) {
return am4core.color("#666666");
}
return fill;
});
推荐阅读
- android - gradle.lang.MissingPropertyException
- python - 哪个生产速度更快?磁盘上的文件与内存中的文件(StringIO,BytesIO)
- javascript - 导入模块时 Web Worker 不工作,即使在使用 worker-loader 时也是如此
- ajax - 在 Rails-React 上 React Ajax 400 错误请求
- php - 代码适用于 php 5 但不适用于 php 7,从类函数返回数组的单个元素
- flask - 如何在 jinja 中存储和访问循环外的值
- python - 如何使用Python列出24个游戏中的所有解决方案
- dataframe - 如何按列值分组
- file - 如何在 Microsoft Edge 浏览器中打开 PDF 文件
- apache-kafka - Kafka:我们可以让消费者订阅同一个主题但主题内有不同的管道吗?