我这里有一个 stackblitz - https://stackblitz.com/edit/ng-tootltip-ocdngb?file=src/app/bar-chart.ts
我在 Angular 应用程序中有一个 D3 图表。
将鼠标悬停在条形图上时会显示工具提示。
在较小的屏幕上,工具提示位于窗口的中央。
要做到这一点,我需要工具提示的 with
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;
关于Angular D3 - 类型 'getBoundingClientRect' 上不存在属性 'Window',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53545382/