angular - 类型 '(err: any, data: any) => void' 与类型 'RequestInit' 没有共同的属性

标签 angular typescript mapbox

我正在关注这个例子 https://www.mapbox.com/mapbox-gl-js/example/timeline-animation/创建基于时间的可视化。 我正在使用这个版本“d3”:“^5.4.0” 代码是:

d3.json('http://127.0.0.1:5000', function (err, data) {
        if (err) throw err;

        // Create a month property value based on time
        // used to filter against.
        data.features = data.features.map(function (d) {
          d.properties.month = new Date(d.properties.time).getMonth();
          return d;
        });

        map.addSource('visits', {
          'type': 'geojson',
          'data': data
        });

        map.addLayer({
          'id': 'visits-circles',
          'type': 'circle',
          'source': 'visits',
          'paint': {
            'circle-color': [
              'interpolate',
              ['linear'],
              ['get', 'name'],
              6, '#FCA107',
              8, '#7F3121'
            ],
            'circle-opacity': 0.75,
            'circle-radius': [
              'interpolate',
              ['linear'],
              ['get', 'name'],
              6, 20,
              8, 40
            ]
          }
        });

        map.addLayer({
          'id': 'visits-labels',
          'type': 'symbol',
          'source': 'visits',
          'layout': {
            'text-field': ['concat', ['to-string', ['get', 'name']], 'm'],
            'text-font': ['Open Sans Bold', 'Arial Unicode MS Bold'],
            'text-size': 12
          },
          'paint': {
            'text-color': 'rgba(0,0,0,0.5)'
          }
        });

        // Set filter to first month of the year
        // 0 = January
        filterBy(0);

        document.getElementById('slider').addEventListener('input', function (e) {
          var month = parseInt(e.target.value, 10);
          filterBy(month);
        });

我对我的数据的 URL 做了完全相同的事情,但我收到了一些错误消息

error TS2559: Type '(err: any, data: any) => void' has no properties in common with type 'RequestInit' error TS2339: Property 'value' does not exist on type 'EventTarget'.

有人知道如何解决吗?

最佳答案

d3 的类型信息暗示了一个基于 promise 的接口(interface)——也许旧版本使用了回调。

您的代码遵循回调模式:

d3.json('http://127.0.0.1:5000', function (err, data) {
    // Handle err

    // Use data
});

这是 promise 版本:

d3.json('http://127.0.0.1:5000')
    .then((data) => {
        // Use data
    })
    .catch((err) => {
        // Handle err
    });

键入响应

返回的data是可以输入的。将类型参数传递给 json 方法以告诉它您将返回哪种数据。例如:

interface ResponseData {
  features: any[];
}

d3.json<ResponseData>('http://127.0.0.1:5000')
.then((data) => {
    // Use data
})
.catch((err) => {
    // Handle err
});

关于angular - 类型 '(err: any, data: any) => void' 与类型 'RequestInit' 没有共同的属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50694455/

相关文章:

node.js - 使用 mongo 生成动态字段(无聚合)

javascript - 将 Mapbox API 与 Angular JS 结合使用时,如何自动完成位置搜索?

java - 获取 map 上的路线点

angular - 运行 Angular 编译器-cli (ngc);找不到模块 '@angular/core'

dart - 在 angular2 中设置自定义 bool 属性的正确语法是什么

javascript - Angular 2 单击时切换 View

javascript - 无法使我的 Highcharts 以 Angular 5 响应

javascript - React、Typescript 项目中 merge 冲突如何有效

Angular 2 - 如何在应用程序模块级别提供服务

iphone - RMMapView setCenterCoordinate 函数不正常