Vue-i18n - Cannot read property 'config' of undefined

自古美人都是妖i 提交于 2021-01-26 09:19:03

问题


First of all I show you what works (in App.js)

import router from './routes.js'; 
import VueI18n from 'vue-i18n';

const messages = {
  en: {
    message: {
      hello: 'hello world'
    }
  }
}

// Create VueI18n instance with options
const i18n = new VueI18n({
  locale: 'en', // set locale
  messages, // set locale messages
})


const app = new Vue({
        el: '#app',
        router,
        i18n
});

But if I want to separate the code in lang.js

import VueI18n from 'vue-i18n';

const messages = {
  en: {
    message: {
      hello: 'hello world'
    }
  }
}

export default new VueI18n({
   locale: 'en', // set locale
   messages, // set locale messages
});

So that I can write in App.js

import router from './routes.js'; 
import i18n from './lang.js'; 

const app = new Vue({
        el: '#app',
        router,
        i18n
});

But somehow this doesn't work even though routes.js is built exact the same.

My bootstrap.js looks like that, if it is important to know.

import Vue from 'vue';

window.Vue = Vue;

import VueRouter from 'vue-router';
import VueI18n from 'vue-i18n';

Vue.use(VueRouter);
Vue.use(VueI18n);

I'm sorry for the long code, but somehow the mistake lies in import i18n from './lang.js'; I get the message: Uncaught TypeError: Cannot read property 'config' of undefined


回答1:


In your main file where you create the app instance add the i18n as option instead of Vue.use(Vuei18n) like this:

new Vue({
  el: '#app',
  i18n,  // < --- HERE
  store,
  router,
  template: '<App/>',
  components: { App }
})

Put it just after el;

And this should be your lang:

import Vue from 'vue'
import VueI18n from 'vue-i18n'
import en from './en'
import fr from './fr'
import ro from './ro'

Vue.use(VueI18n)

export default new VueI18n({
  locale: 'en',
  fallbackLocale: 'en',
  messages: {
    en, fr, ro
  }
})



回答2:


Just to add a detail, you MUST construct the new VueI18n after using Vue.use(VueI18n)

Vue.use(VueI18n);
// must be called after vue.use
const i18n = new VueI18n({
    locale: "en",
    fallbackLocale: "en",
    messages: {
        en
    }
});
new Vue({
    el: "#app",
    i18n,
    render: (h) => h(App),
});



回答3:


The idea of code separation can be implemented this way

/* i18n.ts */

import VueI18n from "vue-i18n";

import en from "./locales/en.json";

// Export as an arrow function
export default () =>
  new VueI18n({
    locale: process.env.VUE_APP_I18N_LOCALE || "en",
    fallbackLocale: process.env.VUE_APP_I18N_FALLBACK_LOCALE || "en",
    messages: { en }
  });
/* plugins.ts */

import Vue from "vue";

// Plugins
import VueI18n from "vue-i18n";

import i18n from "./i18n";

Vue.use(VueI18n);

export default {
  i18n: i18n()
};

And finally, the main.ts

/* main.ts */

import Vue from "vue";

import plugins from "./core/plugins";

import App from "./App.vue";

new Vue({
  ...plugins,
  render: (h) => h(App)
}).$mount("#app");

The trick is using arrow functions when exporting i18n settings. It's worth mentioning that this problem does not occur with some other Vue plugins.



来源:https://stackoverflow.com/questions/48607892/vue-i18n-cannot-read-property-config-of-undefined

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