Composition-api: help: How to access to state of vuex store with @nuxtjs/composition-api

Created on 21 Sep 2020  路  2Comments  路  Source: nuxt-community/composition-api

馃摎 What are you trying to do? Please describe.
I wanted to use vuex store with module mode in nuxtjs. but I couldn't access vuex state of store from context.

馃攳 What have you tried?
My code is like below but I couldn't access state.
Have you looked through the docs? Tried different approaches? The more detail the better.

setup(_, { root }: SetupContext) {
    const theme = computed(() => root.$store.state.global.theme)
    return {theme}
}

By the way, I can make it true with @vue/composition-api.

鈩癸笍 Additional context
I use "@nuxtjs/composition-api": "^0.12.5".
I also use SetupContext type from "@vue/composition-api" installed in "@nuxtjs/composition-api": "^0.12.5"

Most helpful comment

Try this:

import { useContext } from '@nuxtjs/composition-api'

export default {
  setup() {
    const { app: { store } } = useContext()

    const theme = computed(() => store.state.global.theme)

    return { theme }
  }
}

All 2 comments

Try this:

import { useContext } from '@nuxtjs/composition-api'

export default {
  setup() {
    const { app: { store } } = useContext()

    const theme = computed(() => store.state.global.theme)

    return { theme }
  }
}

I got it. I can't use secound arguemnt in setup as context.
It works now. Thank you

Was this page helpful?
0 / 5 - 0 ratings