我使用 mobx 库。它非常适合 ReactJS。我有这样的可观察数组:
@observable items = [];
当我以这种方式添加对象时,我没有问题,并且给定的对象将可以按预期观察。
let x = {
Title: "sample title"
}
items.push(x);
但是当我定义一个强类型对象(使用打字稿)
export class SampleObject {
Title: string;
constructor(title: string) {
this.Title = title;
}
}
并以这种方式推送新对象,它是不可观察的
items.push(new SampleObject("Sample Title"));
我怎么解决这个问题 !?
x 和 y 有什么区别?!
var x = {
Title: "sample"
}
var y = new SampleObject("sample");