Is there a way to get the name of the nameSpaced vuex module inside its action or mutation?
Asked Answered
H

1

10

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' ?

Hardden answered 4/4, 2019 at 10:56 Comment(0)
H
1

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.

Heartsick answered 2/9, 2021 at 12:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.