无法访问类函数内部的私有变量

时间:2019-10-04 11:06:59

标签: angular

我正在从事一个有角度的项目,并设计d3饼图。一切正常。但是我的问题是我无法访问私人课程 函数内部的变量。

2 个答案:

答案 0 :(得分:2)

范围问题。更改

.attr('y', function (d, i) { return (this.legendHeight * (i + 1))})

收件人

.attr('y', (d, i) => { return (this.legendHeight * (i + 1))})

One of many docs

答案 1 :(得分:0)

您面临的问题可以解释为: 在您的function (d, i) { return (this.legendHeight * (i + 1))}中,恰好是this所指的是您所期望的另一个上下文。 请尝试将其更改为箭头符号,例如(d,i) => this.legendHeight * (i + 1),以解决您的问题。

如果您不能使用此表示法,则需要对legendHeight的值进行本地引用