使类在MobX中可观察到

问题描述 投票:1回答:1

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

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个回答
0
投票

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

© www.soinside.com 2019 - 2024. All rights reserved.