错误 TS2322:类型“下面”不可分配给类型“LegendPosition”

DGR*_*DGR 0 typescript angular ngx-charts

我使用 ngx-charts API,以便在我的网络应用程序上用饼图显示数据。当我尝试修改图表的图例位置时,它显示以下错误消息:

“错误 TS2322:类型“下方”不可分配给类型“LegendPosition”。”。

我正在使用 Angular 12 和 ngx-charts API 来使用它漂亮的图表。

DGR*_*DGR 5

我挖了几个小时直到找到解决方案。问题出在字符串本身。当我在 html 文件上写入“[legendPosition]="'below'”时,它只是无法识别它,即请求的内容。

在 html 页面上(在 ngx-charts 标签上):

[legendPosition]="legendPosition"
Run Code Online (Sandbox Code Playgroud)

在其相应的 ts 文件中,导出的类中:

public legendPosition: LegendPosition = LegendPosition.Below;
Run Code Online (Sandbox Code Playgroud)

当然,除非您导入 LegendPosition,否则这将不起作用:

import { LegendPosition } from '@swimlane/ngx-charts';
Run Code Online (Sandbox Code Playgroud)

现在,[lgegentPosition] 将使用 LegendPosition 对象,而不是接收字符串。顺便说一句,你可以看到我使用了“LegendPosition.Below 。这不是呈现图例的唯一方式,而只是一个示例。