vuex is like this
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(){
}
}
})
when the parent component is created
beforeCreate(){
this.$queryProject().then( res => this.$store.commit("pushProjectsToStore",res.data) )
},
when subcomponents are instantiated
mounted () {
//
this.projects = this.$store.getters.getAllProjs;
this.projectNamesById = this.$store.getters.getProjectNamesById;
},
and now there is a problem. The execution time of getAllProjs is before pushProjectsToStore, so we can"t get the data. How to solve this problem?