Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to access Vuex module getters and mutations?

Tags:

vue.js

vuex

I'm trying to switch to using Vuex instead of my homegrown store object, and I must say I'm not finding the docs as clear as elsewhere in the Vue.js world. Let's say I have a Vuex module called 'products', with its own state, mutations, getters, etc. How do I reference an action in that module called, say, 'clearWorking Data'? The docs give this example of accessing a module's state:

store.state.a // -> moduleA's state 

But nothing I can see about getters, mutations, actions, etc.

like image 916
John Moore Avatar asked Jan 24 '17 16:01

John Moore


People also ask

How do you access getters in mutations Vuex?

To access getters within Vuex mutations with Vue. js, we just use the state in the getter and call the getter with the state . //... const store = new Vuex.

Are Vuex getters cached?

Vuex allows us to define "getters" in the store. You can think of them as computed properties for stores. As of Vue 3.0, the getter's result is not cached as the computed property does.

How to access getters within Vuex mutations with Vue JS?

To access getters within Vuex mutations with Vue.js, we just use the state in the getter and call the getter with the state. //... const store = new Vuex.Store ( { //... getters: { foo: (state) => { return getterFunc (state); }, }, mutations: { fooMutation: (state, data) => { getterFunc (state); }, }, //... });

What is a getter in Vuex?

Vuex getters behave a lot like Mongoose getters: they're special properties that are computed from other properties when you access them. For example, suppose your store contains a user's firstName and lastName. You can write a getter that returns the user's fullName. Vuex getters react to changes in the store's state just like computed properties.

How to get the full name of a user in Vuex?

For example, suppose your store contains a user's firstName and lastName. You can write a getter that returns the user's fullName. Vuex getters react to changes in the store's state just like computed properties.

How to pass arguments to getters in Vue?

Note that getters accessed as properties are cached as part of Vue's reactivity system. You can also pass arguments to getters by returning a function. This is particularly useful when you want to query an array in the store: Note that getters accessed via methods will run each time you call them, and the result is not cached.


2 Answers

In Addition to the accepted answer I wanna provide you with a workarround for the getter which is missing in the answer.

Debug the Store
In any case you can call console.log(this.$store) to debug the Store.
If you do so you will see the getters are prefixed with the namespace in their name. enter image description here

Access namespaced getter
this.$store.getters['yourModuleName/someGetterMethod']

Dispatch namespaced
this.$store.dispatch('yourModuleName/doSomething')

Dispatch namespaced with params
this.$store.getters['yourModuleName/someGetterMethod'](myParam)

Conclusion
The key is to handle the namespace like a file System like Justin explained.

Edit: found a nice library for handling vuex Store
In addition to the basic knowledge I'd like to add this vuex library as a nice addition for working effectivly and fast with the vuex store. https://github.com/davestewart/vuex-pathify .
It looks pretty interesting and cares much of the configuration for you and also allows you to handle 2waybinding directly with vuex.

** Edit: Thanks to the other Answers. Added Dispatching method with params for wholeness.

like image 82
Gkiokan Avatar answered Oct 07 '22 23:10

Gkiokan


In your example it would be store.dispatch('products/clearWorkingData') you can think of actions/mutations as a file system in a way. The deeper the modules are nested the deeper in the tree they are.

so you could go store.commit('first/second/third/method') if you had a tree that was three levels deep.

like image 44
Justin MacArthur Avatar answered Oct 08 '22 00:10

Justin MacArthur