Search code examples
javascriptvue.jsvuexstore

How to access Vuex store in Vue setup() method?


How do I access the Vuex store in Vue when using the setup() method?

I don't have access to the regular this.$store variable anymore.


Solution

  • According to the composition API documentation you have to use the following syntax:

    import { computed } from 'vue' // If not already imported
    import { useStore } from 'vuex'
    
    export default {
      setup () {
        const store = useStore()
    
        return {
          // access a state in computed function
          count: computed(() => store.state.count),
    
          // access a getter in computed function
          double: computed(() => store.getters.double)
        }
      }
    }