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

12

参考 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/

我到底做错了什么?从文档中读取时,我看不到任何明显的差异?

3个回答

18

嗯,我不认为这是你的错,但现在没有出错,可惜状态没有映射。 - Micheal J. Roberts
放弃那个,我打错了。谢谢@artoju! - Micheal J. Roberts

2

这是一个提示,表明你的应用程序某个地方正在调用:

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

如果你还没有将Store的模块XYZ定义为"命名空间",那么需要在模块导出中添加namespaced:true来实现:

//XYZ.js

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

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

另外,请检查在Vuex.Store()创建时声明的命名空间键名是否正确,例如:

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

0
export default new Vuex.Store({
   modules: { userstore },
   state: {},
   mutations: {},
   actions: {},
   modules: {},
});

也许您有两个像这样的模块,最后一个会替换第一个。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接