在我的NUXT应用程序中,我正在使用vuex商店模块!当我运行应用程序并打电话
this.$store.dispatch('userStore/setLoggedInUser',currentUser); //default.vue
我得到一个错误,说“不要改变变异处理程序之外的vuex存储状态”,它无限循环!
我的模块看起来像这样:
const actions = {
setLoggedInUser({commit},currentUser){
return new Promise((resolve,reject)=>{
commit('mutateLoggedInUser',currentUser);
resolve();
});
}
}
const mutations = {
mutateLoggedInUser(state,user){
state.loggedInUser = user;
}
}
我找到了解决方案(有点) . 通过在store / index.js文件中将strict mode设置为false,错误消失 .
export const strict = false
但这似乎是一个hacky解决方案,我真的不明白最新情况 . 这是NUXT中的错误还是我在vuex商店做了一些奇怪的事情?
1 回答
如果您正在某处看到传入的值导致无限更新循环,但通常会发生此错误,但如果您没有观看,则可能是由于您传入原始值而不是副本 .
尝试更改:
state.loggedInUser = user
至state.loggedInUser = user.slice()
或this.$store.dispatch('userStore/setLoggedInUser',currentUser.slice())