gpt4 book ai didi

javascript - 从另一个商店调用商店 mobx

转载 作者:行者123 更新时间:2023-11-29 20:59:39 25 4
gpt4 key购买 nike

我有 2 家商店,如下所示

商店 1

 export default class ContactInformationModel {
@observable id;
@observable type;
@observable description;
@observable mode;

constructor(id,type,description,mode,show) {
this.id=id;
this.type=type;
this.description=description;
this.mode=mode;

}
}


export default class ContactInformationController {
@observable contact_informations = [];

addContactInformation(data){
var id= data.hasOwnProperty("id") ? data.id: this.getInsertedId(data.mode); //auto increment for store mvc;
var type=data.type;
var description=data.description;
var mode=data.mode;
this.contact_informations.push(new ContactInformationModel(id,type,description,mode));
}



@computed get SellerContact(){
return this.contact_informations.filter(c => c.mode == 'seller');
}


}

商店 2

import ContactInformationController from  './ContactInformationController';
var contact_information_store=new ContactInformationController();
export default class SellerController {
@observable seller = {}; //only one entry

saveSellerContact(){
//pull contact_information_store
var contact_information=contact_information_store.SellerContact;

}

}

当我打印 contact_information 时,它是空白数组,但它是在 jsx 中呈现的。我仍然是 react/mobx 的新手,我们将不胜感激。谢谢

最佳答案

我认为问题在于您有 ContactInformationController 类的两个不同实例。如果您改为导出 ContactInformationController 的实例,它将得到解决,因此您可以在整个应用程序中使用相同的实例。

class ContactInformationModel {
@observable id;
@observable type;
@observable description;
@observable mode;

constructor(id, type, description, mode, show) {
this.id=id;
this.type=type;
this.description=description;
this.mode=mode;
}
}

class ContactInformationController {
@observable contact_informations = [];

addContactInformation(data){
var id= data.hasOwnProperty("id") ? data.id: this.getInsertedId(data.mode); //auto increment for store mvc;
var type=data.type;
var description=data.description;
var mode=data.mode;
this.contact_informations.push(new ContactInformationModel(id,type,description,mode));
}

@computed get SellerContact(){
return this.contact_informations.filter(c => c.mode == 'seller');
}
}

export { ContactInformationModel };
export default new ContactInformationController();

关于javascript - 从另一个商店调用商店 mobx,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47256947/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com