属性getBBox在SVGElement类型上不存在

时间:2017-08-21 08:39:45

标签: javascript typescript d3.js svg types

我正在使用d3,我想使用SVG元素的getBBox方法。

我正在使用以下内容:(d3Selection.node() as SVGElement).getBBox(),但由于标题中的错误,TypeScript无法编译。

SVGElement是否使用错误的类型?我可以使用any代替它工作,但这看起来像是一个“不干净”的解决方案。

2 个答案:

答案 0 :(得分:3)

并非所有SVG元素都有边界框,<defs>例如没有,<title>,所以是SVGElement是错误的类型。你想要SVGGraphicsElement

答案 1 :(得分:0)

SVGElement不是访问SVGSVGElement的正确类型,而是访问<svg>元素的正确类型,其中getBBox()方法也是可用的,因为它继承自{{1 }}。

SVGGraphicsElement

或者如果(d3Selection.node() as SVGSVGElement).getBBox() 被定义为

d3Selection

可以直接投入使用。