【问题标题】:Make class as observable in MobX使类在 MobX 中可观察
【发布时间】:2019-09-26 23:23:19
【问题描述】:

我有一个类,我想让整个类成为可观察的,然后我将创建该类的实例,然后它将存储在上下文中,因此在存储它之前,我需要将该类作为可观察的,所以我可以观察任何变化:

class PrimitiveContext {
  currentPageId: number;
  primitiveValue: string = 'Testval';

  constructor(
    @inject(OBS_IT.CurrentPageId) currentPageId: number) {
    this.currentPageId = currentPageId;

    this.updateValue = this.updateValue.bind(this);
  }

  getValue() {
    return this.primitiveValue;
  }
}

// Context
const primitive = createContext(new PrimitiveContext()) 

以上是示例代码,如何使 PrimitiveContext 成为可观察的。

【问题讨论】:

    标签: mobx mobx-react


    【解决方案1】:

    尝试使用https://github.com/farwayer/mobx-decorators#allobservable 我在我的项目中使用它,但它不支持 mobx 5+ 版本

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-12-01
      • 1970-01-01
      • 1970-01-01
      • 2020-07-14
      相关资源
      最近更新 更多