【问题标题】:calling store from another store mobx从另一个商店 mobx 调用商店
【发布时间】:2017-11-13 18:16:06
【问题描述】:

我有 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 的新手,任何帮助将不胜感激。谢谢

【问题讨论】:

    标签: javascript reactjs ecmascript-6 mobx mobx-react


    【解决方案1】:

    我认为问题在于您有两个不同的 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();
    

    【讨论】:

      猜你喜欢
      • 2019-11-20
      • 1970-01-01
      • 2021-11-21
      • 1970-01-01
      • 2018-06-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-08-18
      相关资源
      最近更新 更多