HasBert
HasBert

Reputation: 143

Vuex, namepaced module getters not available in root getter?

Namepaced module getters not available in root getter, vuex.

Hey guys,

I have a getter in my root state, this getters needs to reference an other getter (getFilteredSpecialItems) out of a module (filter). I am aware of the rootGetters. I can reference the filter/getFilteredSpecialItems in the vue components, so that's working so far.

root state

const getters: {
  getSortedList: (state, getters, rootState, rootGetters) {
    ...
      console.log(getters);
    console.log(rootGetters);
    console.log(getters.getFilteredSpecialItems);
    console.log(rootGetters.getFilteredSpecialItems);
    console.log(getters.filter.getFilteredSpecialItems);
    console.log(rootGetters.filter.getFilteredSpecialItems);
    ...
    }
}

filter module

const state: {
  namespaced: true
  ...
}

const getters: {
  getFilteredSpecialItems: (state, getters, rootState) => {
    ....
  }
}

folder structure

store
| index.js
| getters.js
| mutations.js
| actions.js
| state.js
| filter (dir)
| \
| | index.js
| | getters.js
| | mutations.js
| | actions.js
| | state.js

Both, the getters and rootGetters have registered the filter module (first and second console.log):

getters getters

rootGetters rootGetters

the 3. and 4. console.log are undefined, the 5. and 6. throw an error, because getters.filter and rootGetters.filter are undefined, so they don't have any properties.

How can I get the getFilteredSpecialItems inside the root getSortedList getter?

I appreciate any kind of help or advise. Pre-Thanks.

Upvotes: 1

Views: 717

Answers (1)

Decade Moon
Decade Moon

Reputation: 34306

You have to access namespaced getters like this:

rootGetters['filter/getFilteredSpecialItems']

not like this (unlike state):

rootGetters.filter.getFilteredSpecialItems

From another getter within the same module, you should be able to do this

getters.getFilteredSpecialItems

If it returns undefined then perhaps your getter is returning undefined? Confirm that it is actually being called, keeping in mind that its last return value may be cached if no observable data within the getter function has changed since the last call.

Upvotes: 2

Related Questions