首页 文章

vuex-persistedstate:如何使用带有vuex-persistedstate的getter?

提问于
浏览
0

我用vuex-persistedstate . 但是吸气者没有数据 . 像这样 .

Vue.js Devtools in Chrome

那么如何使用吸气剂呢?

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 回答

  • 2

    我想你正在访问 state 对象外面 . 请尝试添加 state 作为函数参数:

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

相关问题