简体   繁体   English

Nuxt.js Vuex 模块未按预期工作

[英]Nuxt.js Vuex module not working as expected

I'm having problems with Nuxt's Vuex using it in modules.我在模块中使用 Nuxt 的 Vuex 时遇到问题。

It turns out that the state is being declared and appears in Vuex, the actions are triggered, but the mutation changes the state instance, but do not commit the change and do not even trigger the event, as it does not appear in the Vuex devtools console, below Vuex module code.事实证明, state正在被声明并出现在 Vuex 中,动作被触发,但mutation改变了state实例,但不提交更改甚至不触发事件,因为它没有出现在 Vuex devtools 中控制台,在 Vuex 模块代码下方。

Note: in both console.log() print the state, in the first, empty, as it was declared, and in the second, the changed state, but the change does not really reflect in Vuex.注意:在console.log()中都打印状态,在第一个中,空的,因为它被声明,在第二个中,改变的状态,但变化并没有真正反映在 Vuex 中。

 export const strict = false export const state = () => ({ address: {} }) export const mutations = { setShopAddress(state, address) { console.log(state) state.address = address console.log(state) } } export const actions = { getAddress({ commit }) { this.$axios .get('/general/address') .then((response) => { commit('setShopAddress', response.data) }) .catch((e) => console.error(e)) }, setAddress(vuexContext, address) { vuexContext.commit('setShopAddress', address) } } export const getters = { getShopAddress(state) { return state.address } }

After many hours and thanks to a group of Nuxt on Telegram I was able to find the solution, I just needed to put an async on nuxtServerInit and an await on the call to action.几个小时后,多亏了 Telegram 上的一组 Nuxt,我才能够找到解决方案,我只需要在 nuxtServerInit 上放置一个 async 并在号召性用语上放置一个 await。

Code below:代码如下:

 async nuxtServerInit({ dispatch }) { await dispatch('module/action') },

I hope my answer helps more people希望我的回答能帮助到更多人

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM