为了账号安全,请及时绑定邮箱和手机立即绑定

一个关于vuex state中的数据存取问题?

一个关于vuex state中的数据存取问题?

小唯快跑啊 2018-07-18 19:02:36
vuex的结构是这样的export default new Vuex.Store({    state:{         projects:[],     },     getters:{         getAllProjs(state){             return state.projects;         },         getProjectNamesById(state){             return state.projects.map( proj => proj.name )         }     },     mutations:{         pushProjectsToStore(state,data){            state.projects = data;         },     },     actions:{         pushProjectsToStore(){                      }     } })父组件创建时beforeCreate(){      this.$queryProject().then( res => this.$store.commit('pushProjectsToStore',res.data) )   },子组件实例化时mounted () {    //   获取项目信息       this.projects = this.$store.getters.getAllProjs;      this.projectNamesById = this.$store.getters.getProjectNamesById;   },然后现在有个问题,getAllProjs执行时机是在pushProjectsToStore之前,所以拿不到数据,请问如何解决
查看完整描述

2 回答

?
ABOUTYOU

TA贡献1812条经验 获得超5个赞

computed: {

...mapGetters(["getAllProjs","getProjectNamesById"])

},
watch:{

getAllProjs(newVal,oldVal){
    console.log('vuex 中上一次的值',oldVal);
    console.log('vuex 中更新后的值',newVal);
},
getProjectNamesById(newVal,oldVal){

}

}


查看完整回答
反对 回复 2018-07-20
  • 2 回答
  • 0 关注
  • 1314 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信