在原生小程序里,有什么类似vue的 computed、watch 的方案呢?

在原生小程序里,有什么类似vue的 computed、watch 的方案呢?

我现在用的一个不是很完善,
就拿watch来说,
data.obj改变的话可以监控到,
data.obj.a改变的话就监控不到了

求大佬们分享一下~

阅读 4.3k
3 个回答

如果对基础库没有限制的话,官方有一个computed组件扩展https://github.com/wechat-min...
以下代码摘自官网:

const computedBehavior = require('miniprogram-computed')

Component({
  behaviors: [computedBehavior],
  data: {
    a: 0,
  },
  computed: {
    b() {
      // 计算属性同样挂在 data 上,每当进行 setData 的时候会重新计算
      // 比如此字段可以通过 this.data.b 获取到
      return this.data.a + 100
    },
  },
  methods: {
    onTap() {
      this.setData({
        a: ++this.data.a,
      })
    }
  }
})

可以试试mobx

这么想用vue直接mpvue呗

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题