典型的Mobx模型是具有一组可观察到的键的类,例如:
import { observable, computed } from "mobx";
class OrderLine {
@observable price = 0;
@observable amount = 1;
@computed get total() {
return this.price * this.amount;
}
}
但是,这假设我事先知道我的模型可能具有哪些键。但是在我的用例中,我的模型只有一个固定键,其余的将是动态的-用户将能够添加额外的键。我应该如何定义我的模型?我应该将其设为ObservableMap的子类吗?
import { observable } from "mobx";
class Bookmark {
@observable tags = [];
# This has to allow additional properties to be added to the object but still be observable
# e.g. bookmark.foo = "bar"
}