角度D3-类型“窗口”上不存在属性“ getBoundingClientRect”

ttm*_*tmt 2 d3.js typescript angular

我在这里有一个堆叠闪电-https://stackblitz.com/edit/ng-tootltip-ocdngb ? file = src/app/bar-chart.ts

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

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

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

为此,我需要与工具提示一起使用

const toolTipWidth = tooltip.node().getBoundingClientRect().width;
Run Code Online (Sandbox Code Playgroud)

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

该应用仍在运行,但出现错误

error TS2339: Property 'getBoundingClientRect' does not exist on type 'BaseType'.
  Property 'getBoundingClientRect' does not exist on type 'Window'.
Run Code Online (Sandbox Code Playgroud)

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

Mar*_*mek 5

您可以简单地使用tooltip.node()as any来解决此问题:

const toolTipWidth = (tooltip.node() as any).getBoundingClientRect().width;
Run Code Online (Sandbox Code Playgroud)

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

const toolTipWidth = (tooltip.node() as HTMLElement).getBoundingClientRect().width;
Run Code Online (Sandbox Code Playgroud)