字典对象的 Vue + Vuex 反应性

Vue + Vuex reactivity of dictionary object

我正在使用 Vue + Vuex 来管理状态。

让我们开店:

state: {
  nodes: {}
},
actions: {
  loadNodes ({ commit }, parameter) {
     axios.get('http://URL' + '/nodes', {
        params: {
          name: parameter['name'],
          type: parameter['type']
        }
      }).then((response) => {
        commit('nodeActivity', { uid: parameter['uid'], res: response.data })
    })
  }
},
mutations: {
  nodeActivity (state, data: {uid, res}) {
    Vue.set(state.nodes, data.uid, data.res)
  }
},
getters: {
  getNodes: state => state.nodes
}

这可能没问题,在nodes中有一个字典结构{ id: data }

现在我有了带钩子的组件:

created () {
    this.$store.dispatch('nodeActivity', { uid: this.$props.uid, name: this.$props.namepar, type: this.$props.typepar })
  } 

所以 API 调用获得了一些数据并且是异步存储的。有用。但我需要得到特定的 uid 类似 this.$store.getters.getNodes[0] 的东西,它不起作用,但 this.$store.getters.getNodes 可以正常工作,所以我认为由于 Vue.set(..),反应性还可以。即使我使用 const tmp = this.$store.getters.getNodestmp 包含整个字典)然后 tmp2 = tmp[0],tmp2 也不是反应性的。 我不知道为什么。 我刚开始使用 Vue,所以我很欣赏如何更改商店设计的提示。

好吧,我找到了一个很差而且可能超出设计的解决方案。 我只有 watcher 用于从商店加载数据的变量,类似于 const tmp = this.$store.getters.getNodes。当 tmp 发生变化时,我可以通知并分配变量 tmp2 = tmp[number] 以便行为是反应性的。我觉得这个解决方案很老套。