Vuex:getters 应该是函数,但模块“customer”中的“getters.default”是 {}

Nik*_*her 5 javascript vue.js vuex

我在几个模块中构建了我的 Vuex 存储,现在我面临着一个奇怪的 Vuex 错误,我无法解决:

Uncaught Error: [vuex] getters should be function but "getters.default" in module "customer" is {}.
at assert (vuex.esm.js?358c:97)
at eval (vuex.esm.js?358c:271)
at eval (vuex.esm.js?358c:85)
at Array.forEach (<anonymous>)
at forEachValue (vuex.esm.js?358c:85)
at eval (vuex.esm.js?358c:270)
at Array.forEach (<anonymous>)
at assertRawModule (vuex.esm.js?358c:265)
at ModuleCollection.register (vuex.esm.js?358c:191)
at eval (vuex.esm.js?358c:205)
Run Code Online (Sandbox Code Playgroud)

我的 Vuex 商店的结构基于以下模式构建

- store
-- index.js
-- modules
--- customer
---- index.js
---- actions.js
---- getters.js
---- mutations.js
Run Code Online (Sandbox Code Playgroud)

这是我的基本index.js:

import Vue from 'vue'
import Vuex from 'vuex'
import customerModule from './modules/customer/index'
import globalModule from './modules/global/index'
import projectModule from './modules/project/index'

Vue.use(Vuex)

export default new Vuex.Store({
  modules: {
    customer: customerModule,
    global: globalModule,
    project: projectModule
  }
})
Run Code Online (Sandbox Code Playgroud)

我的模块/客户/index.js:

import * as actions from './actions'
import * as getters from './getters'
import * as mutations from './mutations'

const state = {
  customers: []
}

export default {
  namespaced: true,
  state: state,
  actions: actions,
  mutations: mutations,
  getters: getters
}
Run Code Online (Sandbox Code Playgroud)

我的模块/customer/getters.js:

const customers = state => state.customers

export default {
  customers
}
Run Code Online (Sandbox Code Playgroud)

我不太确定这里发生了什么以及为什么我会收到这个奇怪的错误。

有任何想法吗?

Joh*_*now 4

这对我有用,尽管你所做的是 Vuex 在文档中使用的示例。

使用

import getters from './getters'

代替

import * as getters from './getters'