目前我有一个计算的吸气剂 xyz。为了安排新的计算,我调用notifyProperty(this, #xyz);
.
在最新版本的observe中,notifyProperty
不推荐使用。我该如何更换它?文档建议使用this.notifyPropertyChange(#xyz, oldValue, newValue);
. 问题是,在计算 getter时,我没有oldValue
(而不是直接的)。newValue
目前我有一个计算的吸气剂 xyz。为了安排新的计算,我调用notifyProperty(this, #xyz);
.
在最新版本的observe中,notifyProperty
不推荐使用。我该如何更换它?文档建议使用this.notifyPropertyChange(#xyz, oldValue, newValue);
. 问题是,在计算 getter时,我没有oldValue
(而不是直接的)。newValue
开发人员的建议是将oldValue
周围保存在私有变量中以供参考。至于newValue
你实际上可以只传递getter,它会用新值计算它。
您将看到与此类似的课程:
class MyElement extends Observable {
@observable var foo, bar;
var _oldValue;
@reflectable get xyz => foo + bar;
MyElement() {
// we use the xyz getter to compute its own new value
// notifyPropertyChange returns the new value for convenience :)
notifyXyz() { _oldValue = notifyPropertyChange(#xyz, _oldValue, xyz); }
onPropertyChange(this, #foo, notifyXyz);
onPropertyChange(this, #bar, notifyXyz);
}
}