vuejs / vuex

🗃️ Centralized State Management for Vue.js.
https://vuex.vuejs.org
MIT License
28.42k stars 9.58k forks source link

修改state后watch不调用 #426

Closed mokai closed 8 years ago

mokai commented 8 years ago

使用vuex2,因为把数据源的回调也放在store.js中,在回调中修改state的代码

Vue.set(store.state.history.monthWeights, 1, 10)

在View上边获取

computed: {
  weekWeights: function(){
    return this.$store.state.history.weekWeights
  }
}

然后把weekWeights传到子组件上中,在子组件中watch

watch: {
    'data': {
      handler: function (newData, oldData) {
        console.log('month-watch:' + newData)
      },
      deep: true
    }
  }
vvenv commented 8 years ago

为什么不用 getters? http://vuex.vuejs.org/en/getters.html

monthWeights?还是 weekWeights?

Vue.set 的用法似乎错了 http://vuejs.org/api/#Vue-set

mokai commented 8 years ago

@crossjs 解决了,把原先的数组copy一份,设置新值再覆盖原先的数组就好了,谢谢~