【问题标题】:NuxtJS Vue-i18n - "Cannot read property 'fallbackLocale' of undefined"NuxtJS Vue-i18n - “无法读取未定义的属性‘fallbackLocale’”
【发布时间】:2020-11-18 01:53:03
【问题描述】:

我想在我的 nuxtjs 应用程序中使用 vue-i18n,但我总是收到此错误:

Cannot read property 'fallbackLocale' of undefined

我认为这与我的中间件有关:

//middleware/i18n.js
export default function ({ isHMR, app, store, route, params, error, redirect }) {
  const defaultLocale = app.i18n.fallbackLocale
  // If middleware is called from hot module replacement, ignore it
  if (isHMR) return
  const locale = route.query.lang || defaultLocale
  if (store.state.locales.indexOf(locale) === -1) {
    return error({ message: 'This page could not be found.', statusCode: 404 })
  }
  //Mutate the store's locale once we understand which locale is being requested prior to each page render
  store.commit('SET_LANG', locale)
  // Set locale from the query string '?lang='**''
  app.i18n.locale = store.state.locale  
}

但是在i18n.js中我明确设置了app.i18n.fallbackLocale

//plugins/i18n.js
export const i18n = ({ app, store }) => {
  // inject our i18n instance into the app root to be used in middleware
  // we assume a store/index.js file has been defined and the variable 'locale' defined on store, we'll go into this in detail in the next code snippet
  app.i18n = new VueI18n({ //construction a new VueI18n
    locale: store.state.locale,
    fallbackLocale: 'de',
    messages: {
      //'locales' directory contains all the translations in the form of json files
      'en': require('~/static/locales/en.json'), 
      'de': require('~/static/locales/de.json')
    }
  })
}

【问题讨论】:

    标签: vue.js nuxt.js vue-i18n


    【解决方案1】:

    nuxt.config.js删除:

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

    nuxt.config.js 用法:

        '@/plugins/i18n.js',
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-06-15
      • 1970-01-01
      • 1970-01-01
      • 2019-07-19
      • 1970-01-01
      • 2020-01-17
      • 2019-01-30
      • 1970-01-01
      相关资源
      最近更新 更多