2017-08-29 63 views
0

我只想在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突變,並承諾updateDescription從registeredModule

自定義方法
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

目前的IAM以下這個解決方案link,但它不爲我工作:(

誰能解決這個,,,對不起,通過分量/網頁上的英文不好

回答

0

調用$ store.registerModule() beforeMount

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