Vuex:在 mapState() 中找不到 \[vuex\] 模块命名空间 \| mapGetters(): X/

新手上路,请多包涵

根据用于模块化组织商店的 Vuex 文档( https://vuex.vuejs.org/guide/modules.html ),我有以下内容:

store/index.js :

 import Vue from "vue";
import Vuex from "vuex";

Vue.use(Vuex);

// Default Store State for the Hug Employee Dashboard:
// import defaultState from "@/store/defaultStore";

/*eslint no-param-reassign: ["error", { "props": false }]*/
/*eslint indent: ["error", 2]*/
/*eslint arrow-body-style: ["error", "always"]*/
/*eslint-env es6*/

const surfGroups = {
  state: {
    activeSurfGroup: {},
    fetchingSurfGroupLoading: false,
    creatingSurfGroupLoading: false
  },
  getters: {
    activeSurfGroup: state => {
      return state.activeSurfGroup;
    },
  },
  mutations: {
    // API Request Mutations:
    SET_ACTIVE_SURF_GROUP(state, payload) {
      this.activeSurfGroup = payload;
    },
    // Loading Mutations:
    SET_FETCHING_SURF_GROUP_LOADING(state, payload) {
      state.fetchingSurfGroupLoading = payload;
    },
    SET_CREATING_SURF_GROUP_LOADING(state, payload) {
      state.creatingSurfGroupsLoading = payload;
    }
  },
  actions: {
    fetchSurfGroup: async ({ commit }, payload) => {
      commit("SET_FETCHING_SURF_GROUP_LOADING", true);
      // Dispatches the following Surf Levels API service:
      // this.$surfLevelsAPI.fetchActiveSurfGroup(
      //   payload.activeSurfGroupUUID
      // );
      await Vue.prototype.$surfLevelsAPI.fetchActiveSurfGroup(payload).then((response) => {
        if (response.success) {
          commit("SET_ACTIVE_SURF_GROUP", response.data);
          commit("SET_FETCHING_SURF_GROUP_LOADING", false);
        }
      });
    },
    createSurfGroup: async ({ commit }, payload) => {
      commit("SET_CREATING_SURF_GROUP_LOADING", true);
      // Dispatches the following Surf Levels API service:
      // this.$surfLevelsAPI.createNewSurfGroup(
      //   payload.activeSurfInstructor, payload.activeSurfSessionSelected, payload.activeGroupSurfers
      // );
      await Vue.prototype.$surfLevelsAPI.createNewSurfGroup(payload).then((response) => {
        if (response.success) {
          commit("SET_CREATING_SURF_GROUP_LOADING", false);
        }
      });
    }
  }
};

export default new Vuex.Store({
  modules: {
    surfGroups: surfGroups
  }
});

我觉得这一切看起来都很好。

但是,在 component.Vue 中,当我 …mapState 为:

 import { mapState } from 'vuex';

export default {
  name: "MyComponent",
  // Computed Properties:
  computed: {
    ...mapState('surfGroups', [
      'creatingSurfGroupLoading',
    ]),
  },
  // Component Watchers:
  watch: {
    creatingSurfGroupLoading() {
      console.log(this.creatingSurfGroupLoading);
    },
  },
};

我收到以下错误:

[vuex] module namespace not found in mapState(): surfGroups/

我到底做错了什么?我可以通过阅读文档看到任何明显的差异吗?

原文由 Micheal J. Roberts 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 1.9k
2 个回答

您尚未设置名称空间。

 const surfGroups = {
  namespaced: true,
...

来源: https ://vuex.vuejs.org/guide/modules.html#namespacing

原文由 artoju 发布,翻译遵循 CC BY-SA 4.0 许可协议

这表明您正在从应用程序的某个地方调用:

 mapState("XYZ..")
//or
magGetters("XYZ..")
//or
this.$store.dispatch('XYZ/myFunc'...
this.$store.commit('XYZ/myFunc'...

虽然您还没有将商店的模块 XYZ 定义为“命名空间”。为此,在模块导出中,添加 namespaced:true

 //XYZ.js

export default {
    namespaced: true,
    ...
    getters:{
        myFunc: state => {
            return "HELLO";
       }
    }
    mutations,
    actions,
};

因此,不需要直接在 Vuex.Store({..}) 中进行更改。它会自动将该模块识别为 namespaced


ps 还检查您在 Vuex.Store() 创建中声明的名称空间键名称是否正确,例如:

 Vuex.Store({
    modules:{
       abc: XYZ   <---- i.e. key name should be `XYZ` , that matters mostly.
    }
})

原文由 T.Todua 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题