在 Nuxt 应用中使用 Vuex 经典模式
Use Vuex classic mode in Nuxt application
我正在将 Vue 应用程序重写为 Nuxt 架构,因为我们需要 SSR。但是我不想重写 Vuex 存储文件,它是:
import Vue from "vue";
import Vuex from "vuex";
import vuexI18n from "vuex-i18n/dist/vuex-i18n.umd.js";
import toEnglish from "../translations/toEnglish";
import toSpanish from "./../translations/toSpanish";
import toGerman from "./../translations/toGerman";
import toRussian from "./../translations/toRussian";
Vue.use(Vuex);
const store = new Vuex.Store({
state: {
currentLanguage: ''
},
mutations: {
changeLang: (state, response) => {
if(response) {
state.currentLanguage = response;
Vue.i18n.set(response);
console.log(response);
}
}
}
});
Vue.use(vuexI18n.plugin, store);
Vue.i18n.add("en", toEnglish);
Vue.i18n.add("es", toSpanish);
Vue.i18n.add("de", toGerman);
Vue.i18n.add("ru", toRussian);
export default store;
我知道 Nuxt 有一些其他方法,但我真的想坚持使用上面的代码。不幸的是,我无法通过以下方式从我的组件调用突变:
this.$store.commit('changeLang', lang)
它在控制台中打印错误:
[vuex] unknown mutation type: changeLang
我也这样试过
this.$store.commit('store/changeLang', lang)
但错误是一样的。如何解决?我是否需要重写此 vuex 文件才能使其正常工作?
我按照@Aldarund 的提示将上面的代码更改为:
import Vue from "vue";
import Vuex from "vuex";
import vuexI18n from "vuex-i18n/dist/vuex-i18n.umd.js";
import toEnglish from "../translations/toEnglish";
import toSpanish from "./../translations/toSpanish";
import toGerman from "./../translations/toGerman";
import toRussian from "./../translations/toRussian";
const store = () => {
return new Vuex.Store({
state: () => ({
currentLanguage: ''
}),
mutations: {
changeLang: (state, response) => {
if (response) {
state.currentLanguage = response;
Vue.i18n.set(response);
console.log(response);
}
}
}
})
};
Vue.use(vuexI18n.plugin, store);
Vue.i18n.add("en", toEnglish);
Vue.i18n.add("es", toSpanish);
Vue.i18n.add("de", toGerman);
Vue.i18n.add("ru", toRussian);
export default store;
现在错误是
Uncaught TypeError: store.registerModule is not a function
可能是因为Vue.use(vuexI18n.plugin, store);
.
您需要使用经典模式。
Classic (deprecated): store/index.js returns a method to create a
store instance
所以在导入 Vue 时应该是这样的,没有使用 vuex。而且它必须是 crestr 存储的函数,而不是普通的 vuex 对象
import Vuex from 'vuex'
const createStore = () => {
return new Vuex.Store({
state: () => ({
counter: 0
}),
mutations: {
increment (state) {
state.counter++
}
}
})
}
export default createStore
文档https://nuxtjs.org/guide/vuex-store/#classic-mode
至于插件,例如vyexi18 你需要将该代码移动到插件文件中并从上下文 https://nuxtjs.org/guide/plugins/
中创建存储对象
export default ({ store }) => {
Your vuex18initcode
}
我正在将 Vue 应用程序重写为 Nuxt 架构,因为我们需要 SSR。但是我不想重写 Vuex 存储文件,它是:
import Vue from "vue";
import Vuex from "vuex";
import vuexI18n from "vuex-i18n/dist/vuex-i18n.umd.js";
import toEnglish from "../translations/toEnglish";
import toSpanish from "./../translations/toSpanish";
import toGerman from "./../translations/toGerman";
import toRussian from "./../translations/toRussian";
Vue.use(Vuex);
const store = new Vuex.Store({
state: {
currentLanguage: ''
},
mutations: {
changeLang: (state, response) => {
if(response) {
state.currentLanguage = response;
Vue.i18n.set(response);
console.log(response);
}
}
}
});
Vue.use(vuexI18n.plugin, store);
Vue.i18n.add("en", toEnglish);
Vue.i18n.add("es", toSpanish);
Vue.i18n.add("de", toGerman);
Vue.i18n.add("ru", toRussian);
export default store;
我知道 Nuxt 有一些其他方法,但我真的想坚持使用上面的代码。不幸的是,我无法通过以下方式从我的组件调用突变:
this.$store.commit('changeLang', lang)
它在控制台中打印错误:
[vuex] unknown mutation type: changeLang
我也这样试过
this.$store.commit('store/changeLang', lang)
但错误是一样的。如何解决?我是否需要重写此 vuex 文件才能使其正常工作?
我按照@Aldarund 的提示将上面的代码更改为:
import Vue from "vue";
import Vuex from "vuex";
import vuexI18n from "vuex-i18n/dist/vuex-i18n.umd.js";
import toEnglish from "../translations/toEnglish";
import toSpanish from "./../translations/toSpanish";
import toGerman from "./../translations/toGerman";
import toRussian from "./../translations/toRussian";
const store = () => {
return new Vuex.Store({
state: () => ({
currentLanguage: ''
}),
mutations: {
changeLang: (state, response) => {
if (response) {
state.currentLanguage = response;
Vue.i18n.set(response);
console.log(response);
}
}
}
})
};
Vue.use(vuexI18n.plugin, store);
Vue.i18n.add("en", toEnglish);
Vue.i18n.add("es", toSpanish);
Vue.i18n.add("de", toGerman);
Vue.i18n.add("ru", toRussian);
export default store;
现在错误是
Uncaught TypeError: store.registerModule is not a function
可能是因为Vue.use(vuexI18n.plugin, store);
.
您需要使用经典模式。
Classic (deprecated): store/index.js returns a method to create a store instance
所以在导入 Vue 时应该是这样的,没有使用 vuex。而且它必须是 crestr 存储的函数,而不是普通的 vuex 对象
import Vuex from 'vuex'
const createStore = () => {
return new Vuex.Store({
state: () => ({
counter: 0
}),
mutations: {
increment (state) {
state.counter++
}
}
})
}
export default createStore
文档https://nuxtjs.org/guide/vuex-store/#classic-mode
至于插件,例如vyexi18 你需要将该代码移动到插件文件中并从上下文 https://nuxtjs.org/guide/plugins/
中创建存储对象export default ({ store }) => {
Your vuex18initcode
}