使用vue的ElementUI庫,在多語言時報錯:
TypeError: Cannot read property '_t' of undefined
錯誤是在點菜單欄時隨機拋出的,F12抓不到,只是報錯提示。
vue => 2.6.10
element-ui => 2.7.2
vue-i18n => 8.15.0
配置和文件路徑如下(index.js):
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
頁面翻譯功能正常。
經過多次嘗試,問題出現的原因是:
在js中使用了如下代碼進行翻譯:
this.$t('...')
解決辦法:
引入index.js中的i18n,在頁面中采用i18n.t替換this.$t('...')
// 引入i18n import i18n from '@/i18n/index' // js中使用如下代替this.$t i18n.t('...')
需要說明的是,在template中的this.$t不用更改,更改后會報錯。
我的問題是這樣解除的,希望能幫到大家!
為什么我也不理解,希望大神能夠指點,多謝!