类型错误:无法读取未定义的属性“getter" [英] TypeError: Cannot read property 'getters' of undefined
问题描述
我正在尝试测试一个引用 Vuex 商店的基本 Vue 组件.我以为我遵循了 Vue 的例子(https://vue-test-utils.vuejs.org/guides/using-with-vuex.html#mocking-getters) 到 T 但它似乎不起作用.
I'm trying to test a basic Vue Component that makes reference to a Vuex store. I thought I followed Vue's example (https://vue-test-utils.vuejs.org/guides/using-with-vuex.html#mocking-getters) to a T but it doesn't appear to be working.
我收到标题中提到的错误.
I get the error that is mentioned in the title.
const localVue = createLocalVue()
localVue.use(Vuex)
describe('Navbar.vue', () => {
let store: any
let getters: any
beforeEach(() => {
getters: {
isLoggedIn: () => false
}
store = new Vuex.Store({
getters
})
})
it('renders props.title when passed', () => {
const title = 'Smart Filing'
const wrapper = shallowMount(Navbar, {
propsData: { title },
i18n,
store,
localVue,
stubs: ['router-link']
})
expect(wrapper.text()).to.include(title)
})
})
我正在使用类组件,所以这可能与它有关吗?
I'm using class components so maybe that has something to do with it?
@Component({
props: {
title: String
},
computed: mapGetters(['isLoggedIn'])
})
export default class Navbar extends mixins(Utils) {}
提前致谢.
推荐答案
这里的getter"没有正确分配:
The "getters" here is not properly assigned:
beforeEach(() => {
getters: {
isLoggedIn: () => false
}
store = new Vuex.Store({
getters
})
})
它应该是 getters = {...
而不是 getters: {...
因为你对 beforeEach 的参数是一个函数而不是一个对象.
It should be getters = {...
rather than getters: {...
because your argument to beforeEach is a function and not an object.
我可以确认它确实在文档中正确写入.
I can confirm that it is indeed correctly written in the documentation.
祝你好运!
这篇关于类型错误:无法读取未定义的属性“getter"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!