Search code examples
javascriptvue.jsstorecomputed-properties

State computed properties not accesible in other computed properties?


In vuex store how can I access a computed property inside other computed properties (getters) ?

This is how my state object looks like:

state: {
  a: 5
},

getters: {

  propA(state){
    return state.a; // ok
  },

  propB(state){
    return state.propA; // undefined
  }

}

If I try to access state.propA from propB I get undefined...


Solution

  • getters are passed as the second argument, so it would be:

    propB (state, getters) {
      return getters.propA;
    }
    

    See https://vuex.vuejs.org/api/#getters

    Whereas components smash all properties together so that this.blah might come from data, computed or props the Vuex store keeps the different parts separate so you always have to be explicit about exactly what you're accessing.