ES6:属性“ selected”在类型“ Object”上不存在

时间:2019-01-16 10:00:01

标签: angular typescript amcharts

从“ 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;并传递了function(fill, target, k)之类的变量,并试图捕获类似k.dataItem.dataContext.selected的值,这给了我更多的错误。

1 个答案:

答案 0 :(得分:2)

您可以尝试这样的事情吗?

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