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

标签 angular typescript d3.js

我这里有一个 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/

相关文章:

javascript - 如何链接 eventemitter next 方法?

javascript - Angular - 如何使用一个函数来管理多个切换/点击事件?

javascript - 日历热图 : how to define the range of % for each color instead of using quantiles

javascript - 使用 D3.js 创建可变数量的元素

javascript - NVD3(D3 JS)如何去掉y2轴

angular - 如何禁用nz-zorro-antd的select控件的下拉菜单?

javascript - 在谷歌地图上添加多个标记

javascript - 如何使用 javascript 或 typescript 在新窗口中打开页面?

Angular 2 : How to modify content of the element with custom directive?

angular - 如何在 angular rc.5 中引入的新 bootstrapModule 中使用旧版提供程序和指令