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

标签 javascript vue.js vuejs2 state vuex

我只想在 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

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

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

然后这是我的自定义方法,它将调用 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})
      }
    }

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

目前我正在遵循此解决方案 link , 但它对我不起作用:(

任何人都可以解决这个问题,抱歉,英语不好

最佳答案

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

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

关于javascript - vuex 注册模块中的未知突变类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45933913/

相关文章:

javascript - 在 Canvas 中查找线的长度

javascript - 在不关闭 websockets 的情况下更改 Firefox 中的 window.location.href

javascript - 输入框不允许输入

javascript - 使用 mousemove 事件转换 jQuery 眼睛瞳孔跟踪器

javascript - Vuejs 上的回调函数

javascript - React 无法对未安装的组件执行状态更新

sorting - BootstrapVue表: sort by date and by string?

javascript - VueJS : clicking disabled button still changes route

css - 如何从 Vue 组件中的 node_modules 导入 css 文件

javascript - Vue MultiSelect改变指针类显示