Nuxt.js中的Vuex默认存储

ars1skjm  于 2023-05-07  发布在  Vue.js
关注(0)|答案(2)|浏览(141)

我的应用程序在vue.js中,现在我想在nuxt.js中重写它,我的应用程序中也有vuex store,对于nuxt.js,vuex store语法是不同的,有没有办法在nuxt.js中使用我的默认存储而不改变它?谢谢你,对不起我的英语:))

brgchamk

brgchamk1#

没什么不同你不会显著改变它。
你所要做的就是把它放到单独的js文件中,然后稍微重写一下:

export const state = () => {

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

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

将其命名为index.js并将其放入“store”文件夹。就这样。就像以前那样用。别激动

z18hc3ub

z18hc3ub2#

// ./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)
  },
}

相关问题