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

Viewed 2100

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.

1 Answers

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

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)
    }
  }
}
Related