【发布时间】:2017-10-29 11:21:05
【问题描述】:
我正在尝试改进 vuex 模块,但出现以下错误:
Uncaught Error: [vuex] getters should be function but "getters.getComments" in module "comments" is [].
/stores/cmets.js(模块)
import Vue from 'vue';
import Vuex from 'vuex';
Vue.use(Vuex);
const state = {
comments: []
}
const getters = {
getComments: state => state.comments
}
const mutations = {
setComments(state, comments) {
state.comments = comments
}
}
const actions = {
setComments(context, data) {
context.commit('setComments', data)
}
}
export default new Vuex.Store({
state,
getters,
mutations,
actions
})
这是我的 store.js,其中包含 vuex 的根状态 store.js
import Vue from 'vue';
import Vuex from 'vuex';
import commentsModule from './stores/comments'
Vue.use(Vuex);
const state = {
}
const getters = {
}
const mutations = {
}
const actions = {
}
export default new Vuex.Store({
state,
getters,
mutations,
modules: {
comments: commentsModule
},
actions
})
你能帮我解决这个问题吗?试过但不明白是什么问题?
【问题讨论】:
-
不应在模块文件中创建新的
Store,然后将新存储导出到假定存储本身。商店本身的代码丢失了。 -
@LiranC 实际上,它并没有丢失。我没有分享 store.js 因为它只包含里面的模块
-
我认为你应该包含它,有些奇怪。
-
@LiranC 我已经在帖子中添加了它
标签: javascript vue.js vuex