Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Vuex with Nuxt.js - The right way

I try to add Vuex to my Nuxt.js project. I made a custom ligthbox and to share through all pages I write the code into the default layout page.

From a page, to hide or show the ligthbox I need to change the state value. This is all the modules into the "store" folder:

// state.js
export const state = () => ({
    lightbox: false,
});
// getters.js
export const getters = {
    getLightbox(state)
    {
        return state.lightbox
    },
}
// actions.js
export const actions = {
    showLightbox({commit})
    {
        commit('UPDATE_LIGHTBOX', true)
    },
    hideLightbox({commit})
    {
        commit('UPDATE_LIGHTBOX', false)
    },
}
// mutations.js
export const mutations = {
    UPDATE_LIGHTBOX(state,value)
    {
        state.lightbox = value
    },
}

Then when I try to call "this.$store.state.lightbox" from a method I get an 500 error:

TypeError: Cannot read property 'getters' of undefined

Even I try to use this way and I get the same error:

import { mapGetters } from 'vuex'
export default {
    computed: {
        ...mapGetters(['getLightbox']),
        lightboxState()
        {
            return this.getLightbox
        },
    }
    mounted()
    {
        console.log( this.lightboxState )
    }
}

Later I try with asyncData but the console print an "undefined" value:

export default {
    asyncData({ store })
    {
        console.log('Store =',store);
    }
}

So, what is the right way to using Vuex with Nuxt.js? Maybe I need to add something else to "nuxt.config.js"?

like image 299
junihh Avatar asked Jun 21 '20 21:06

junihh


People also ask

Can I use Vuex in Nuxt?

Conclusion. Working with Vuex in a Nuxt application is simple and easy to get started with. It provides module creation based on folder and file structure under the store folder.

Is Nuxt better than Vue?

Nuxt offers better SEO improvement with its server-side rendering feature, faster development with an auto-generic router, public share features, and management with great configuration options and meta tags methods, automatic code splitting with pre-rendered pages — all of this is impossible or extremely complex to ...

How to use nuxt plugins with Vuex?

Highly recommend reading the top 5 plugins used for Vuex. Simply create an store/index.js and import them there as recommended by their respective configurations. Nuxt by default configures Vuex to use strict mode in development. It can be disabled in store/index.js if you want, but doing so is a bad idea.

Where can I Find my Vuex files?

All Vuex files are located inside the store folder. Just like the pages folder, it's special. The files and folders are used as build time to automatically generate your Vuex store.

Is Vuex the best state management for nuxt?

There are many great implementations of state management in the JS world, but Nuxt has embraced Vuex and for now it works great. Having a source of truth for state in a user interface saves a lot of time in development.

What is nuxt JS and why should I use it?

First, right out of the box, Nuxt.js gives you a great structure for your code, which is also flexible and configurable in the next.config.js file. Nuxt.js optimizes your code and facilitates proper indexing of your application by search engines.


1 Answers

If you're using separate files for state.js, getters.js, mutations.js and actions.js, they should only have a single, default export.

So instead of

// getters.js
export const getters = ...

you should use

// getters.js
export default {
  getLightbox: state => state.lightbox
}

This applies to all the files in your store directory.

See https://nuxtjs.org/guide/vuex-store/#modules-mode

like image 162
Phil Avatar answered Oct 15 '22 09:10

Phil