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

一世执手 提交于 2019-12-14 03:11:07

问题


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 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');
}

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;

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:

TypeError: Cannot read property 'set' of undefined

this which I consoled also inside mutation is:


回答1:


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

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



来源:https://stackoverflow.com/questions/54565138/not-able-to-access-i18-plugin-from-mutation-in-classic-mode-store-in-nuxt-applic

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!