角度 D3 - 类型 'Window' 上不存在属性'getBoundingClientRect'



我在这里有一个堆栈-https://stackblitz.com/edit/ng-tootltip-ocdngb?file=src/app/bar-图表.ts

我有一个D3图表和角度应用程序。

将鼠标悬停在条形图上时,条形图将显示工具提示。

在较小的屏幕上,工具提示位于窗口的中心。

要做到这一点,我需要使用获得的工具提示

const toolTipWidth = tooltip.node().getBoundingClientRect().width;

这在这里很好,但我实际的应用程序是Angular cli应用程序

应用程序仍然运行,但我得到错误

error TS2339: Property 'getBoundingClientRect' does not exist on type 'BaseType'.
Property 'getBoundingClientRect' does not exist on type 'Window'.

是错误吗?我可以停止吗?

您可以简单地将tooltip.node()强制转换为any来绕过这个问题:

const toolTipWidth = (tooltip.node() as any).getBoundingClientRect().width;

正确的类型应该是HTMLElement,这也应该起作用:

const toolTipWidth = (tooltip.node() as HTMLElement).getBoundingClientRect().width;

最新更新