typescript - D3.js:“this”隐含类型“any”,因为它没有类型注释
问题描述
我使用 3D.js 库:
d3.select(`#myGraph`)
.append('svg')
const svg = d3.select(`#myGraph svg`).append('g')
const nodeEnter = svg.selectAll("g.node")
.data(nodes)
.enter()
.append("g")
.attr("class", "node")
.call(force.drag())
svg.selectAll(".node")
.each(function (d: any) {
d3.select(this)
.attr("data-source", d.id)
})
但我得到了错误:'this' 隐含类型'any',因为它没有类型注释
没有 @ts-ignore 怎么解决?
解决方案
您可以将类型注释添加到this
函数中:
svg.selectAll(".node")
.each(function (this: any, d: any) {
d3.select(this)
.attr("data-source", d.id)
})
或者,如果您不想要明确的any
,您可以将其定义为SVGGraphicsElement
,这是一种通用的 SVG 元素类型。如果您想更详细,可以使用更具体的类型(例如SVGRectElement
或SVGGElement
)来更好地描述选择。
svg.selectAll(".node")
.each(function (this: SVGGraphicsElement, d: any) {
d3.select(this)
.attr("data-source", d.id)
})
推荐阅读
- python - 我如何制作一个机器人,如果你做出反应,它将赋予 ua 与 python 的角色?
- java - 'mvn' 不是 Windows 中的内部或外部命令可运行程序或批处理文件
- amazon-web-services - SNS Should I use subscriptions per integration type/endpoint or Subscriptions per Client?
- javascript - 如何使用字段作为来自一个 graphql 查询的过滤器来获取位于单独查询中的流体图像?
- ios - 无法将用户选择的图像上传到 Firebase 存储并下载 url 以存储在数据库中
- c++ - qt opencv can't read the image
- laravel - laravel migration raise 在 docker 下找不到驱动
- c# - 正则表达式 如果找到则获取结果,如果没有则获取另一个结果
- javascript - 如何折叠按在数组上的项目 - 反应原生?
- python - 我的代码中的字典和随机生成的字符串问题