vue project, when the route jumps quickly, vue-18n has a certain probability of reporting an error Cannot read property'_ t'of null.
reason (my own understanding):
page An and page B are configured in multiple languages, such as
$t ('xxx')
in html, this.$t (' xxx')
in data or method.
when page A jumps to page B, the multilingual loading of page An is not finished but has already jumped to page B, so the this in page A cannot be found.
solution:
multilingual configuration file (my project is i18n/index.js under src)
import Vue from 'vue'
import VueI18n from 'vue-i18n'
import zhLocale from 'element-ui/lib/locale/lang/zh-CN'
import enLocale from 'element-ui/lib/locale/lang/en'
import jaLocale from 'element-ui/lib/locale/lang/ja'
Vue.use(VueI18n) //
let messages = {
'zh-CN': { ...require('./zh'), ...zhLocale },
'en-US': { ...require('./en'), ...enLocale },
'ja-JP': { ...require('./ja'), ...jaLocale },
}
const i18n = new VueI18n({
locale: 'zh-CN', //
messages
})
export default i18n
change this.$t ('xxx') to i18n.t (' xxx') from i18n
page to i18n.t ('xxx')
when configuring a single page with multiple languages.
import i18n from '@/i18n/index'