Angular 类型';(错误:任意,数据:任意)=>;无效';没有与类型';相同的属性;RequestInit';
我按照这个例子来创建一个基于时间的可视化。 我正在使用此版本“d3”:“^5.4.0” 代码是:Angular 类型';(错误:任意,数据:任意)=>;无效';没有与类型';相同的属性;RequestInit';,angular,typescript,mapbox,Angular,Typescript,Mapbox,我按照这个例子来创建一个基于时间的可视化。 我正在使用此版本“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 = da
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做了完全相同的事情,但我收到了一些错误消息
错误TS2559:Type'(err:any,data:any)=>void'没有属性
与类型“RequestInit”错误TS2339相同:属性“value”没有
“EventTarget”类型上不存在
有人知道如何解决这个问题吗?d3的类型信息建议使用一个基于承诺的接口——可能旧版本使用回调 您的代码遵循回调模式:
d3.json('http://127.0.0.1:5000', function (err, data) {
// Handle err
// Use data
});
以下是承诺版本:
d3.json('http://127.0.0.1:5000')
.then((data) => {
// Use data
})
.catch((err) => {
// Handle err
});
类型化响应
您返回的数据
可以键入。将一个类型参数传递给json
方法,告诉它您将得到什么类型的数据。例如:
interface ResponseData {
features: any[];
}
d3.json<ResponseData>('http://127.0.0.1:5000')
.then((data) => {
// Use data
})
.catch((err) => {
// Handle err
});
接口响应数据{
特征:任何[];
}
d3.json('http://127.0.0.1:5000')
。然后((数据)=>{
//使用数据
})
.catch((错误)=>{
//处理错误
});
我也有同样的问题。解决方法是使用
(d3 as any)
例如:
(d3 as any).json(options.data, (error, d) => {
我已经编辑了问题并添加了必要的代码。谢谢。我尝试了您提到的方法,现在我得到了以下错误“error TS2339:Property'value'在类型'EventTarget'上不存在”和“error TS2339:Property'features'在类型{}上不存在”。我已经更新了答案,以演示如何传递类型参数来获取响应数据的类型信息。注释的第二部分实际上是一个新问题,但答案可能是结合使用
this.value
和元素的窄类型(getElementById
返回宽HTMLElement
类型,您需要将其窄到HTMLInputElement
)。在这里的“元素类型”下还有更多内容:我现在得到了错误:core.js:1448 error error:Uncaught(承诺中):error:options无效这意味着服务器不支持使用options
HTTP方法的跨源请求。您需要响应选项
请求才能允许此操作。我正在开始使用typescript和所有相关的东西,我真的不明白这个想法,您能给我解释一下吗