使用vue的ElementUI库,在多语言时报错:javascript
TypeError: Cannot read property '_t' of undefinedhtml
错误是在点菜单栏时随机抛出的,F12抓不到,只是报错提示。vue
vue => 2.6.10java
element-ui => 2.7.2element-ui
vue-i18n => 8.15.0ui
配置和文件路径以下(index.js):this
import Vue from 'vue' import VueI18n from 'vue-i18n' Vue.use(VueI18n) import zh from './config/zh' import en from './config/en' const i18n = new VueI18n({ locale: localStorage.getItem('locale') || 'zh', messages: { zh, en } }) export default i18n
页面翻译功能正常。spa
通过屡次尝试,问题出现的缘由是:翻译
在js中使用了以下代码进行翻译:code
this.$t('...')
解决办法:
引入index.js中的i18n,在页面中采用i18n.t替换this.$t('...')
// 引入i18n import i18n from '@/i18n/index' // js中使用以下代替this.$t i18n.t('...')
须要说明的是,在template中的this.$t不用更改,更改后会报错。
个人问题是这样解除的,但愿能帮到你们!
为何我也不理解,但愿大神可以指点,多谢!
原文出处:https://www.cnblogs.com/SamNicole1809/p/11951454.html