【发布时间】:2019-04-03 10:11:45
【问题描述】:
每当我使用 axios 响应将数据发送到 vuex 存储时,例如。
** Sidebar.vue **
created(){
this.getRoles();
},
methods: {
getRoles(){
var _this = this
var roles = null
this.$http.get('/api/userroles/userroles').then(function(response){
// Passing data to vuex within response
_this.$store.dispatch('setUserRoles', response.data.data)
}
}
check_role(){
}
}
当我像 console.log(this.$store.state.userStore.roles) 这样在侧边栏中进行控制台时它具有价值,但是当我在仪表板中控制台时它返回 null 并且当我看到 vuex chrome 扩展它包含值(下图),但在仪表板中它返回 null
eg in dashboard
** dashboard.vue **
created(){
console.log(this.$store.state.userStore.roles)
// null
},
现在,当我在 axios 响应之外分派到 vuex 时,它可以完美运行并在仪表板中提供价值,但它会抛出错误,例如 Error: [vuex] Do not mutate vuex store state outside mutation handlers。例如。
created(){
this.getRoles();
},
methods: {
getRoles(){
var _this = this
var roles = null
this.$http.get('/api/userroles/userroles').then(function(response){
_this.roles_data = response.data.data
}
_this.$store.dispatch('setUserRoles', _this.roles_data)
}
}
例如在仪表板中,如果我在 axios 之外使用调度,它会提供价值
** dashboard.vue **
created(){
console.log(this.$store.state.userStore.roles)
// (25) [{…}, {…}, {…}, __ob__: Observer]
},
我错过了什么吗???
【问题讨论】:
标签: javascript vue.js vuex