How to change another module state from one module in Vuex?

Sometimes, we want to change another module state from one module in Vuex.

In this article, we’ll look at how to change another module state from one module in Vuex.

How to change another module state from one module in Vuex?

To change another module state from one module in Vuex, we can call commit with the mutation name with the root option set to true.

For instance, we write

commit("TOGGLE_LOADING", null, { root: true });

to call commit to commit the TOGGLE_LOADING mutation with root set to true.

This lets is commit TOGGLE_LOADING no matter which module it’s in.

If the modules are namespaced, we can add the namespace before the mutation name.

For instance, we write

commit("loading/TOGGLE_LOADING", null, { root: true });

to commit the TOGGLE_LOADING mutation that’s in the loading namespace.

Conclusion

To change another module state from one module in Vuex, we can call commit with the mutation name with the root option set to true.