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

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

我正在尝试在我的 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 store 的经典模式:

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: Cannot read property 'set' of undefined

this 我也在突变中安慰的是:

我在突变中将 this.i18n.set(response); 更改为 state.i18n.locale = response;,现在它似乎有效了。

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