useStore

Access this.$store with the Nuxt Composition API.

Vuex v4 provides a helper function for accessing it within the Composition API. This helper provides an equivalent while using Nuxt 2

useStore

Returns this.$store.

import { defineComponent, useStore } from '@nuxtjs/composition-api'

export default defineComponent({
  setup() {
    const store = useStore()
  },
})

You can also provide an injection key or custom type to get back a semi-typed store:

import { defineComponent, useStore } from '@nuxtjs/composition-api'

export interface State {
  count: number
}

export const key: InjectionKey<Store<State>> = Symbol()

export default defineComponent({
  setup() {
    const store = useStore(key)
    const store = useStore<State>()
    // In both of these cases, store.state.count will be typed as a number
  },
})
Edit this page on GitHub Updated at Tue, Mar 2, 2021