Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to use Vuex Store in layouts #331

Open
munjalpatel opened this issue Dec 7, 2020 · 3 comments
Open

Unable to use Vuex Store in layouts #331

munjalpatel opened this issue Dec 7, 2020 · 3 comments

Comments

@munjalpatel
Copy link

I am trying to use Vuex Store in my layout but can't figure out how to make it work.
Here is what I am doing:

store/sources.ts

import { VuexModule, Module, Mutation, Action } from 'vuex-module-decorators'
import { store } from '@/store'
import axios from 'axios'

const { sourcesEndpoint } = process.env

interface Source {
  id: String
}

@Module({
  name: 'sources',
  namespaced: true,
  stateFactory: true,
  dynamic: true,
  store,
})
export default class Sources extends VuexModule {
  private _sources: Source[] = []

  get sources(): Source[] {
    return this._sources
  }

  @Mutation
  updateSources(sources: Source[]) {
    this._sources = sources
  }

  @Action({ commit: 'updateSources' })
  async fetchSources() {
    // eslint-disable-next-line no-console
    console.log(`!!! ${sourcesEndpoint} !!!`)
    return sourcesEndpoint ? await axios.get(sourcesEndpoint) : []
  }
}

store/index.ts

import { Store } from 'vuex'

export const store = new Store({})

layouts/default.vue

<script>
import { getModule } from 'vuex-module-decorators'
import Sources from '@/store/sources'

export default {
  fetch() {
    const sourcesModule = getModule(Sources, this.$store)
    sourcesModule.fetchSources()
  },

  fetchOnServer: false,
}
</script>

And the error I get:

[vuex] must call Vue.use(Vuex) before creating a store instance.
@hrebet
Copy link

hrebet commented Jun 23, 2021

@munjalpatel, Hello!

Tell me please, is there any progress with this? This is a big problem for me now.

@hrebet
Copy link

hrebet commented Jun 24, 2021

I found a temporary solution.

"Lazy" loading of components that use Store.

layouts/default.vue

<template>
  <Component :is="componentWithStore" />
</template>

<script lang="ts">
import { Component, Vue } from 'nuxt-property-decorator'

@Component()
export default class extends Vue {
  get componentWithStore (): () => Promise<any> {
    return () => import('./ComponentWithStore.vue')
  }
}
</script>

Your suggestions?

@riaanduplessis
Copy link

Are you actually calling Vue.use(Vuex) somewhere before the store is instantiated as the error message suggested?

[vuex] must call Vue.use(Vuex) before creating a store instance.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants