使用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