如何解决mobx 挑战:将 getter 和 setter 放入一个可观察的数组
我正在尝试将 getter 和 setter 写入一个可观察的数组,但它不起作用。下面的代码给了我以下错误:Error: [MobX] No annotations were passed to makeObservable,but no decorator members have been found either
我尝试了不同的装饰器组合,但似乎没有任何效果。我想要的行为是每当 AppModel.text 更新时,任何渲染文本获取器的 UI 都应该更新。此外,每当在对象上调用 gonext() 时,从 AppModel.text 渲染的任何 UI 都应更新并渲染来自数组上新 0 项的数据。
class DataThing
{
@observable text?: string = "foo";
}
class AppModel
{
get text() { return this.items[0].text}
set text(value: string | undefined) { this.items[0].text = value;}
items: DataThing[] = observable( new Array<DataThing>());
constructor() {
makeObservable(this);
this.items.push(new DataThing());
}
gonext() { this.items.unshift(new DataThing()); }
}
编辑:
我最终做了以下事情,但仍然想了解如何以可观察的方式索引数组。
class DataThing
{
@observable text?: string = "zorp";
constructor(){makeObservable(this);}
}
class AppModel
{
@observable _current?:DataThing;
get current() {return this._current;}
items: DataThing[] = observable( new Array<DataThing>());
constructor() {
makeObservable(this);
this.gonext();
}
gonext() {
this.items.unshift(new DataThing());
this._current = this.items[0];
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。