Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
4.0k views
in Technique[技术] by (71.8m points)

vue,store中维护的复杂对象被修改后,watch无法监听到

项目中需要在 store 中维护一个如下结构的对象 listObj: {}


listObj: {
    //"xxx": [],
    //"yyy": []
}

其中key/value键值对为动态新增的;value为数组

mutations 方放如下:

    SET_listObj: (state, {key, list}) => {
      let exList = state.listObj[key] || []
      if(exList && exList.length > 0) {
        exList.splice(exList.length)
      }
      state.listObj[key] = list
    },

调用方页面如下:

  computed: {
    ...mapGetters(['listObj']),
  },
  watch: {
    listObj: {
        handler(val) {
            //断点无法进入......
            console.log(val)
        },
        deep: true
    },
  },

当 store 中的listObj进入新增 key/value时,如上监听始终无法执行


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
import Vue from 'vue';

SET_listObj: (state, {key, list}) => {
  if (!state.listObj[key]){
    Vue.set(state.listObj, key, []);
  }
  let exList = state.listObj[key]
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...