After importing a Vuex module, everything is working smoothly without any issues. However, as soon as I try to namespace it, an error message pops up saying that the action is "not a function" when it is called.
This is how it looks like in index.js:
import Vue from 'vue'
import Vuex from 'vuex'
import MainWindow from './modules/MainWindow';
import ArtEditor from './modules/ArtEditor';
Vue.use(Vuex);
export default new Vuex.Store({
state: {
},
mutations: {
},
actions: {
},
modules: {
MainWindow,
ArtEditor
}
});
The ArtEditor module code:
const state = {
selectedColor : "#FFFFFF"
}
const getters = {
selectedColor: state => state.selectedColor
};
const actions = {
selectColor({commit}, newColor){
commit('selectColor', newColor);
console.log(newColor)
}
};
const mutations = {
selectColor: (state, newColor) => state.selectedColor = newColor
};
export default {
namespaced: true,
state,
getters,
actions,
mutations
};
In a Vue component:
import {mapActions, mapGetters} from 'vuex';
...
methods:{
...mapActions(['ArtEditor/selectColor']),
colorChanged(color){
this.selectColor(color);
}
},
...
Whenever the color changes, an error occurs with the message
this.selectColor is not a function
. The confusion arises as most of the documentation on namespaced modules either does not import external Vuex modules or uses dispatch()
instead of mapActions()
. There seems to be something amiss that I cannot figure out.