无法从Nuxt应用程序的经典模式存储中的突变访问i18插件

时间:2019-02-07 01:43:20

标签: vue.js vuejs2 internationalization vuex nuxt.js

我正在尝试在Nuxt应用程序中实现Vuex i18n软件包。在插件数组的nuxt.conf.js文件中,我有:

{
    src: '@/plugins/i18n.js',
    ssr: false
},

plugins / i18n.js文件为:

import Vue from "vue";
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";

export default ({ store }) => {
    Vue.use(
        vuexI18n.plugin,
        store,
        {
            onTranslationNotFound: function (locale, key) {
                console.warn(`vuex-i18n :: Key '${key}' not found for locale '${locale}'`)
            }
        }
    );

    // register the locales
    Vue.i18n.add('en', toEnglish);
    Vue.i18n.add('de', toGerman);
    Vue.i18n.add('es', toSpanish);

    // Set the start locale to use
    Vue.i18n.set('de');
    Vue.i18n.fallback('en');
}

最后一件事是我的商店。我正在Nuxt中使用vuex存储的经典模式:

import Vuex from "vuex";

const store = () => {
    return new Vuex.Store({
        state: () => ({
            currentLanguage: ''
        }),
        mutations: {
            changeLang(state, response) {
                if (response) {
                    console.log(this);
                    state.currentLanguage = response;
                    this.i18n.set(response);
                }
            }
        }
    })
};


export default store;

正如您在存储文件中看到的那样,我正在尝试使用this关键字访问i18n插件。不幸的是,控制台出现打印错误:

  

TypeError:无法读取未定义的属性“ set”

this我也在突变内得到安慰的是:

Not able to access i18 plugin from mutation in classic mode store in nuxt application

1 个答案:

答案 0 :(得分:0)

我在自己的突变中将this.i18n.set(response);更改为state.i18n.locale = response;,现在看来可以正常工作。

由于某种原因,当我将此突变称为我的video.js播放器刷新时。我将尝试找出原因。