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"?
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.
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 ...
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.
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.
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With