To access the getter from another Vuex module, we can get them from the rootGetters parameter. const store = new Vuex. Store({ //... getters: { someGetter: (state, getters, rootState, rootGetters) => { //...
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.
Vuex getter is not reactive.
In VueJS 2.0, you must pass both state
and getters
.
Getters are passed to other getters as the 2nd Argument:
export default foo = (state, getters) => {
return getters.yourGetter
}
Official documentation: https://vuex.vuejs.org/guide/getters.html#property-style-access
Pass getters
as the second argument to access local and non-namespaced getters. For namespaced modules, you should use rootGetters
(as the 4th argument, in order to access getters defined within another module):
export default foo = (state, getters, rootState, rootGetters) => {
return getters.yourGetter === rootGetters['moduleName/getterName']
}
Getters receive other getters as the 2nd argument
getters: {
doneTodos: state => {
return state.todos.filter(todo => todo.done)
},
doneTodosCount: (state, getters) => {
return getters.doneTodos.length
}
}
Here is a link to the official docs - https://vuex.vuejs.org/guide/getters.html#property-style-access
I Tested without state
and didn't work. That's why state
is necessary.
this works:
export default foo = (state, getters) => {
return getters.yourGetter
}
this didn't work
export default foo = (getters) => {
return getters.yourGetter
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With