代码之家  ›  专栏  ›  技术社区  ›  Niladri Banerjee - Uttarpara

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

  •  1
  • Niladri Banerjee - Uttarpara  · 技术社区  · 6 年前

    从“amcharts4 codepen pre-selecting areas”中,我想将JS转换为ES6。但是,我出错了

    错误TS2339:类型“object”上不存在属性“selected”。

    我要转换的代码如下:

    // 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 回复  |  直到 6 年前
        1
  •  2
  •   Julien METRAL    6 年前

    你能试试这个吗?

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