Reputation: 347
I have multiple vuex modues with nameSpaced: true in my application. Inside the action or mutations of the modules I want to know the name of the parent module.
say I have 2 modules, 'a' and 'b', I call the mutation like
this.$store.commit("a/setName", name);
this.$store.commit("b/setName", name);
Now inside the setName function, I want to know what the calling nameSpace is ? whether it is 'a' or 'b' ?
Upvotes: 10
Views: 794
Reputation: 1426
Currently this is still an open issue for vuex
, as you can see here: Access module namespace from within module #1244. There are a lot of workarounds for this request, but I want to show you an easy one according to your provided code.
This:
this.$store.commit("a/setName", name);
this.$store.commit("b/setName", name);
... could be this:
this.$store.dispatch('a/setName', {namespace: "a", data: name})
this.$store.dispatch('b/setName', {namespace: "b", data: name})
Yes, that means that you need to manually apply the name of that namespace you are calling, but in your example you already did it. In your action it could be something like this:
setName({commit}, object) {
console.log(object.namespace); // submitted namespace
commit('SET_THIS_NAME', object.data); // your data
}
Please note, this is only a workaround, as there still isn´t an official possibility to access the namespace inside the vuex
module. And so this isn´t an official way, but one way of workaround.
Upvotes: 1