收藏
回答

mobx为什么有一堆undefined?

代码片段

https://developers.weixin.qq.com/s/v2gsYhmS7Zg1

我要把this.data.arr 传给后台, 但是里面有一堆undefined, 是我使用方式不对吗?

最后一次编辑于  2020-04-02
回答关注问题邀请回答
收藏

1 个回答

  • lgb
    lgb
    2020-05-15

    延迟更新与立刻更新

    为了提升性能,在 store 中的字段被更新后,并不会立刻同步更新到 this.data 上,而是等到下个 wx.nextTick 调用时才更新。(这样可以显著减少 setData 的调用次数。)

    如果需要立刻更新,可以调用:

    • this.updateStoreBindings() (在 behavior 绑定 中)
    • this.storeBindings.updateStoreBindings() (在 手工绑定 中)


    2020-05-15
    有用
    回复 4
    • 柏杨👾
      柏杨👾
      2020-05-15
      调用了this.storeBindings.updateStoreBindings(), 打印出来的还是一堆undefined?
      2020-05-15
      回复
    • lgb
      lgb
      2020-05-15回复柏杨👾
      你的代码提示
      [mobx.array] Attempt to read an array index (0) that is out of bounds (0). Please check length first. Out of bound indices will not be tracked by MobX
      打印的时候给数组加索引

      store.js

      export const store = observable({
        // 数据字段
        arr: ['1'],
      })

      index.js
      test() {
          //this.storeBindings.updateStoreBindings()
          console.log(this.data.arr[0])
          // console.log(this.data.arr.$mobx.values)
        }
      2020-05-15
      回复
    • 柏杨👾
      柏杨👾
      2020-05-15
      如果打印的是一个字符串就没问题, 如果打印的是一个对象,他就会挂到代理上面, 不知道为什么
      2020-05-15
      回复
    • 柏杨👾
      柏杨👾
      2020-05-15
      加索引没问题是因为打印的是一个字符串
      2020-05-15
      回复
登录 后发表内容
问题标签