Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I call a getter from another getter in Vuex?

Tags:

vuejs2

vuex

People also ask

How do I get getter from another Vuex module?

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) => { //...

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

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
}