小程序 mobx-miniprogram-bindings 如何实时更新数据?

作者站长头像
站长
· 阅读数 16

为什么调用 updates 方法,不能实时更新 data 内的数据(19行),而是需要加上 setTimeout 才可以实时更新(21行)

小程序 mobx-miniprogram-bindings 如何实时更新数据?

index.js

updates() {
    this.update()
    console.log("update => 19", this.data.global)
    setTimeout(() => {
      console.log("update => 21", this.data.global)
    }, 10);
  }

global.js

import { observable, action } from "mobx-miniprogram";


const numc = 0;
export const global = observable({
  numA: 1,
  numB: 2,
  numC: numc,


  get sum() {
    return this.numA + this.numB;
  },


  update: action(function () {
    const sum = this.sum;
    this.numA = this.numB;
    this.numB = sum;
  }),
});
回复
1个回答
avatar
test
2024-07-16

已解决

updates() {
    this.update();
    this.updateStoreBindings();
  }
回复
likes
适合作为回答的
  • 经过验证的有效解决办法
  • 自己的经验指引,对解决问题有帮助
  • 遵循 Markdown 语法排版,代码语义正确
不该作为回答的
  • 询问内容细节或回复楼层
  • 与题目无关的内容
  • “赞”“顶”“同问”“看手册”“解决了没”等毫无意义的内容