javascript 注册模块中的 vuex 未知突变类型

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/45933913/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 06:19:44  来源:igfitidea点击:

vuex unknown mutation type in registered module

javascriptvue.jsvuejs2statevuex

提问by cahyowhy

i just wanna create dynamic registered module in vuex, but it seems doesnt work. this is my store file

我只是想在 vuex 中创建动态注册模块,但它似乎不起作用。这是我的商店文件

import Vuex from 'vuex'
import descriptionModule from './module/descriptionModule';
const {state: stateModule, getters, mutations} = descriptionModule;

const createStore = () => {
  return new Vuex.Store({
    state: {
      descriptions: [],
    },
    mutations: {
      addDescriptions(state, payload){
        state.descriptions.push(state.descriptions.length + 1);
        createStore().registerModule(`descriptionModule${payload}`, {
          state: stateModule,
          getters,
          mutations,
          namespaced: true // making our module reusable
        });
      }
    }
  })
};

export default createStore

and this is my custom module that i will registered

这是我将注册的自定义模块

const state = () => {
  return {description: ''}
};
const getters = {
  description: (state) => state.description
};
const mutations = {
  updateDescription(state, payloads){
    state.description = payloads;
  }
};
export default {
  state,getters,mutations
}

and then this is my custom methods that will call addDescriptions mutation and commit updateDescription from registeredModule

然后这是我的自定义方法,它将调用 addDescriptions 突变并从registeredModule 提交 updateDescription

beforeMount(){
  console.log("hahahaha");
  this.$store.commit('addDescriptions', this.id);
},
... more code ....
methods: {
      onType(editor, content){
        console.log(this.$store.state.a);
        console.log(this.$store.state);
        console.log(this.$store);
        this.$store.commit(`descriptionModule${this.id}/updateDescription`, content, {root: true})
      }
    }

every onTypecalled, i get an error unknown mutation type: descriptionModuleeditor1/updateDescriptionin browser.

每次onType调用时,我都会unknown mutation type: descriptionModuleeditor1/updateDescription在浏览器中收到错误消息。

currently iam following this solution link, but it doesnt work for me :(

目前我正在关注此解决方案链接,但它对我不起作用:(

can anyone solve this,,, sorry for bad english

谁能解决这个问题,,抱歉英语不好

回答by cahyowhy

invoke $store.registerModule()via component/pages on beforeMount():

$store.registerModule()通过组件/页面调用beforeMount()

beforeMount(){
      this.$store.registerModule(`descriptionModule${this.id}`, {
        state: stateModule,
        getters,
        mutations,
        namespaced: true // making our module reusable
      });
    },