Javascript 我可以用Mobx store生成可观察变量吗?
初始化存储时,我有一个变量,其中创建了一个新的子存储。重新初始化此子存储时,observer组件不会重新引发更改 范例Javascript 我可以用Mobx store生成可观察变量吗?,javascript,reactjs,mobx,mobx-react,Javascript,Reactjs,Mobx,Mobx React,初始化存储时,我有一个变量,其中创建了一个新的子存储。重新初始化此子存储时,observer组件不会重新引发更改 范例 class OrderStore { price: PriceStore; // not observable @action.bound async fetch(){ const response = await fetchOrder(); this.price = new PriceStore(response.price)
class OrderStore {
price: PriceStore; // not observable
@action.bound
async fetch(){
const response = await fetchOrder();
this.price = new PriceStore(response.price);
}
@action.bound
registerRefreshOrder(){
setInterval(()=>{
this.fetch();
}, 10000);
}
}
class PriceStore{
@observable amount:number;
constructor(priceResponse:{amount:number}){
this.amount = priceResponse.amount;
}
}
因此,虽然价格变量不可见,但观察者组件并没有更新PriceStore.amount更改
对不对?我应该初始化存储一次还是可以重新初始化?当我创建store的可观察实例时,它可以观察到该对象的所有字段或唯一链接?看起来我找到了解决方案。对于可观察的存储实例,需要使用@observable.ref