[Vuejs]-Vuex default store in Nuxt.js

0👍

It’s not different. You don’t change it significantly.

All you have to do is put it into separate js file and a little rewrite:

export const state = () => {

  return {
    cart: [],
    other_variables:{},
  }
}

export const mutations = {
// put all mutations here
}

name it index.js and put it into "store" folder. That’s it. Just use it as before. Easy.

0👍

// ./store/global.js OR you can use any name for js file 
export const state = () => ({
  loader: false,
})

export const mutations = {
  setLoader(state, bool) {
    state.loader = bool
  },
}

export const getters = {
  getLoader: state => state.loader,
}

// get variable from store
computed: {
  loader(){ return this.$store.getters['global/getLoader'] },
}

// set value for loader
this.$store.commit('global/setLoader', true)

// for example if you will work in other store files ./store/requests.js and you need to change loader value you can use this code
export const actions = {
  someRequest({commit, state, dispatch}){
    this.commit('global/setLoader', true)
    setTimeout(() => {
     this.commit('global/setLoader', false)
    }, 2000)
  },
}

Leave a comment