【发布时间】: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')
}
})
}
【问题讨论】: