Angular D3 - 属性'getBoundingClientRect'在类型“Window”上不存在

5
我这里有一个StackBlitz链接 - https://stackblitz.com/edit/ng-tootltip-ocdngb?file=src/app/bar-chart.ts 我在一个Angular应用程序中使用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'.

这是一个错误,我能停止它吗?
1个回答

11

你可以简单地将 tooltip.node() 强制转换为 any 类型来解决这个问题:

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

正确的类型应该是HTMLElement,这也应该可以工作:

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接