TypeError: undefined is not an object (evaluating 'i18n._t')

背景

今天寫vue+element單元測試報了錯

ERROR LOG: '[Vue warn]: Error in render: "TypeError: undefined is not an object (evaluating 'i18n._t')"

解決

Unit tests not working since introducing vue-i18n

大佬是這樣回覆的

You need to inject an i18n object when instantiating your component.

Using your example :

import Vue from 'vue'
import Home from '@/components/Home'

import VueI18n from 'vue-i18n'
Vue.use(VueI18n)
const i18n = new VueI18n({});

describe('Home.vue', () => {
  it('should render correct contents', () => {
    const Constructor = Vue.extend(Home)
    const vm = new Constructor({i18n}).$mount()
    expect(vm.$el.querySelector('h1').textContent)
      .to.equal('Welcome to Server Client Project (STP)!')
  })
})

加上去就好了

最後最好加個封裝 util類 https://stackoverflow.com/questions/48238906/vue-project-tests-are-failing-when-i-added-vue-i18n-karma-mocha-phantomjs/48239256#48239256

發佈了220 篇原創文章 · 獲贊 237 · 訪問量 85萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章