首页 > 解决方案 > 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这给了我更多错误。

标签: angulartypescriptamcharts

解决方案


你可以尝试这样的事情:

polygonTemplate.adapter.add("fill", function(fill, target) {
    const ctx = target.dataItem.dataContext as any;
    if (ctx && ctx.selected) {
        return am4core.color("#666666");
    }
    return fill;
});

推荐阅读