(已解决)TypeError: Cannot read property '_t' of undefined

使用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不用更改,更改后会报错。

我的问题是这样解除的,希望能帮到大家!

为什么我也不理解,希望大神能够指点,多谢!

原文地址:https://www.cnblogs.com/SamNicole1809/p/11951454.html