I have this Vuex module:
//modules/things.js
const state = {
firstThing: 'abc',
secondThing: 'def',
};
const getters = {
getFirstThing: state => state.firstThing,
getSecondThing: state => state.secondThing,
};
const mutations = {
setFirstThing: (state, payload) => state.firstThing = payload,
setSecondThing: (state, payload) => state.secondThing = payload
};
const actions = {};
export default {
namespaced: true, // <------
state,
mutations,
actions,
getters
};
I use namespaced: true
flag and can work with this module like this:
this.$store.state.things.firstThing // <-- return abc here
this.$store.commit('things/setFirstThing', 10)
this.$store.getters['things/getFirstThing'] // <-- return abc here
If I will use constants like in Vuex official example, and refactor my modules/things.js
file like this:
export const Types = {
getters: {
GET_FIRST_THING: 'GET_FIRST_THING',
GET_SECOND_THING: 'GET_SECOND_THING',
},
mutations: {
SET_FIRST_THING: 'SET_FIRST_THING',
SET_SECOND_THING: 'SET_SECOND_THING',
}
};
const getters = {
[Types.getters.GET_FIRST_THING]: state => state.firstThing,
[Types.getters.GET_SECOND_THING]: state => state.secondThing,
};
const mutations = {
[Types.mutations.SET_FIRST_THING]: (state, payload) => state.firstThing = payload,
[Types.mutations.SET_SECOND_THING]: (state, payload) => state.secondThing = payload
};
I will have to use namespace prefix:
this.$store.commit('things/' + Types.mutations.SET_FIRST_THING, 10);
this.$store.getters['things/' + + Types.getters.GET_FIRST_THING]
If I will include module namespace prefix to Types
constant, I will have to use string prefix things/
for mutations/actions/getters declaration:
const getters = {
['things/' + Types.getters.GET_FIRST_THING]: state => state.firstThing,
['things/' + Types.getters.GET_SECOND_THING]: state => state.secondThing,
};
How to avoid that?
import removeNamespace from '../namespace-helper'
in the store but then _actions is of NamespaceHelper type and doesn't have _getter or the other properties. – Carrissa