0
votes

I use vuex-persistedstate. But getters doesn't work. I checked state in Vue.js Devtools, state was present. But getters didn't have data. Like this.

Vue.js Devtools in Chrome

So how can I use getters?

index.js in Vuex

import createPersistedState from 'vuex-persistedstate'

Vue.use(Vuex)

export default new Vuex.Store({
  modules: {
    login,
    segments,
    itemList,
    orderList
  },
  plugins: [
    createPersistedState()
  ]
})

index.js in vue-router (I want to use getters for login status check)

import store from '@/store'

Vue.use(Router)

const ifAuthenticated = (to, from, next) => {
  if (store.getters.loginStatus) {
    next()
  } else {
    next({
      path: '/login',
      query: { redirect: to.fullPath }
    })
  }
}

login.js (store module in Vuex)

const state = {
  loginStatus: false,
  email: '',
  isStaff: false,
  company: '',
  token: ''
}
const getters = {
  loginStatus: () => state.loginStatus,
  email: () => state.email,
  isStaff: () => state.isStaff,
  company: () => state.company,
  token: () => state.token
}
1
Can you include your getters code?ittus
Yes. I added getters code in the question.Jun

1 Answers

5
votes

I think you're accessing outside state object. Please try to add state as function parameter:

   const getters = {
      loginStatus: (state) => state.loginStatus,
      email: (state) => state.email,
      isStaff: (state) => state.isStaff,
      company: (state) => state.company,
      token: (state) => state.token
    }