所以让我们假设我的类(class)是这样的:
import { computedFrom } from 'aurelia-framework';
export class Test {
myObj;
@computedFrom('myObj.myArray')
get someProperty() {
debugger;
return JSON.stringify(this.myObj ? this.myObj.myArray : this.myObj);
}
attached() {
this.myObj = {
myArray: []
};
setTimeout(() => this.myObj.myArray.push('foo'), 500);
}
}
我的 HTML 只是:
<template>
<h3>myArray value: ${someProperty}</h3>
</template>
我遇到一个问题,当我将元素插入 myObj.myArray
时,someProperty
未更新。我使用 someProperty
getter 中的调试器验证了这一点。当 this.myObj
最初在 attached
中设置并且随后调用 someProperty
getter 时,我看到 myObj
是this.__observers__
中的 SetterObserver
。
但是,我在 this.myObj.__observers__
中没有看到任何 myArray
的 ModifyCollectionObserver
(事实上,__observers__
属性甚至不存在)。我希望数组上会有某种观察者,因为它在我的 compulatedFrom
装饰器中被指定为依赖项。
最佳答案
您可以将您的 compulatedFrom
装饰器更改为以下内容,事情就会开始工作:
@computedFrom('myObj.myArray.length')
这是因为当您将项目插入数组时,length
属性将会发生变化,并且 Aurelia 可以观察到该变化。
关于Aurelia - 没有由computeFrom 装饰器引用的嵌套数组的ModifyCollectionObserver?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43371385/