本文介绍了无法从Nuxt应用程序的经典模式存储中的突变访问i18插件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to implement Vuex i18n package within my Nuxt application. In my nuxt.conf.js file in plugins array I have:

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

plugins/i18n.js文件为:

plugins/i18n.js file is:

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存储的经典模式:

Last thing is my store. I'm using classic mode of vuex store in Nuxt:

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插件.不幸的是,控制台出现打印错误:

As you can see in store file in mutation I'm trying to access i18n plugin with this keyword. Unfortunetally in print error in console:

我在变体内部也安慰过的

this是:

this which I consoled also inside mutation is:

推荐答案

在我的突变中,我将this.i18n.set(response);更改为state.i18n.locale = response;,现在看来可行.

I changed this.i18n.set(response); to state.i18n.locale = response; inside my mutation and now it seems working.

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

For some reason when I call this mutation my video.js player refresh. I will try to find out why.

这篇关于无法从Nuxt应用程序的经典模式存储中的突变访问i18插件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-24 03:52