我正在关注这个例子 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/