我想在方法中增加一个计数器,如下所示:
row(title, height):: {
_panels:: [],
_panelSlot:: 0,
addPanel(panel):: self {
self._panelSlot = self._panelSlot + 1,
_panels+: [{
slot: panelSlot,
panel: panel,
}],
},
该行self._panelSlot = self._panelSlot +1
是我期望的工作,但这确实会导致以下错误:
STATIC ERROR: lib/grafana/builder.libsonnet:157:7-11: unexpected: self while parsing field definition
我的第二次尝试:
我不确定这是否会覆盖该_panelSlot
变量,但我无法确认,因为我无法访问我的对象数组上的该变量:
row(title, height):: {
_panels:: [],
// height is the row's height in grid numbers and it is used' to calculate the
// panel's height and y pos accordingly.
_height:: height,
// panelSlot is the row panels' current position from left-to-right. If there are
// two or more stacked panels they share the same slot. This is used to calculate
// the width for each panel.
_panelSlot:: 0,
addPanel(panel):: self {
local parent = self,
_panelSlot:: self._panelSlot + 1,
_panels+: [{
slot: parent._panelSlot,
panel: panel,
}],
},
RUNTIME ERROR: max stack frames exceeded.
...
<anonymous>
lib/grafana/builder.libsonnet:(13:9)-(15:10) thunk <array_element>
lib/grafana/builder.libsonnet:19:29-35 object <anonymous>
lib/grafana/builder.libsonnet:19:15-37 thunk <array_element>
lib/grafana/builder.libsonnet:(7:24)-(20:6) object <anonymous>
During manifestation
我的问题
如何修改方法中的变量(即递增计数器),以便在下次调用该方法时可以访问修改后的变量?