Search code examples
vue.jsvue-componentvuex

With Vuex, why isn't mapGetters accepting the same syntax as mapState?


I have a Vue component that maps in state, mutations, actions, and getters from a Vuex store.

import {mapState, mapMutations, mapActions, mapGetters} from 'vuex'
export default {
  name: 'DefaultLayout',
  computed: {
    ...mapState({
      settings: (state) => state.settings,
      language: (state) => state.language
    }),
    ...mapState([
      'changeRouteTo'
    ]),
    ...mapGetters([
      'isLoggedIn'
    ])
  }
...

The problem is, I cannot get ...mapGetters to work with the explicit syntax like I do with the first instance of ...mapState above.

I've tried

...mapGetters({
  isLoggedIn: (state) => state.getters.isLoggedIn
})

and

...mapGetters({
  isLoggedIn: (state) => state.isLoggedIn
})

and

...mapGetters({
  isLoggedIn: (state) => this.$store.getters.isLoggedIn
})

But only

...mapGetters([
  'isLoggedIn'
])

seems to work.


Solution

  • To use an object in ...mapGetters the syntax is as follows:

    ...mapGetters({
      isLoggedIn: 'isLoggedIn'
    })
    

    where the key is the name you want the getter to map to and the value is the name of the getter as a string