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

时间:2019-12-04 13:04:14

标签: vue.js vuex

在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/

我到底做错了什么?通过阅读文档,我可以发现任何明显的不足之处吗?

2 个答案:

答案 0 :(得分:1)

您尚未设置名称空间。

const surfGroups = {
  namespaced: true,
...

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

答案 1 :(得分:1)

这表明在您的应用程序中,您正在呼叫:

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


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

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