获取错误未捕获错误:[vuex] getters 应该是函数,但“getters.getters”是 {}。当存储被分成模块时在 VUEX 存储中

gau*_*han 5 javascript getter vue.js vuex

我是 VUEX 新手,在学习 VUEX 的同时构建了一个测试应用程序。我已将 VUEX 存储分成多个模块,每个模块都有自己的 getter.js 文件。Getters、actions、mutations 被导入到每个模块的单独的 index.js 中,并进一步导入到主存储的 index.js 文件中。我收到错误“未捕获错误:[vuex] getters 应该起作用,但“getters.getters”是 {}。” 显示网站时在浏览器上。我也试图用地图助手来实现这一点

有人可以帮我解决这个错误吗?文件内容的详细信息如下

文件夹结构:

store
 modules
  coaches
   action.js
   getter.js
   action.js
   index.js
  requests
   action.js
   getter.js
   action.js
   index.js
 index.js
Run Code Online (Sandbox Code Playgroud)

coaches/index.js 文件内容为:

import { createStore } from "vuex";
import mutations from "./mutations.js";
import actions from "./actions.js";
import getters from './getter.js';

export default createStore({
  namespaced: true,
  state: {
      coaches: [
        {
          id: "c1",
          firstname: "Gau",
          lastname: "Rau",
          area: "[finance,javascript,analysis]",
          description: "devloper for fun",
          age: "38",
        },
        {
          id: "c2",
          firstname: "Ran",
          lastname: "Bi",
          area: "[insurance,SQL,analysis]",
          description: "photographer for fun",
          age: "37",
        }
      ]
    },
  mutations: mutations,
  actions: actions,
  getters: {getters},
});

**coaches/getter.js file content:**

   

     export default {
            coacheslist(state){
                return state.coaches;
            },
            hasCoaches(state){
                return state.coaches && state.coaches.length >0;
            }
        };

**store/index.js file content is:**

import { createStore } from "vuex";
import CoachModule from "./modules/coaches/index.js";
import RequestModule from "./modules/requests/index.js";

export default createStore({
  modules: {
    Coaches: CoachModule, 
    Requests: RequestModule}
});

**File content which calls the getter:**

<script>
export default {
  computed: {
    filteredCoaches(){
      return this.$store.getters['Coaches/coacheslist']
    }
  }
}
</script>
Run Code Online (Sandbox Code Playgroud)

sta*_*eth 3

看起来问题在于您将 getter 定义为具有 1 个参数的对象getters。只是改变它getters: getters,或者更好地拥抱 ES6 并写成这样

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