Javascript Vue-组件中无法访问存储模块

Javascript Vue-组件中无法访问存储模块,javascript,vue.js,frontend,vuex,Javascript,Vue.js,Frontend,Vuex,我的src/store/modules/authToken.js文件如下所示: const authToken = { state: { token: localStorage.getItem('user-token') || '', status: '', }, mutations: { authSuccess(state, token) { console.log('hellllo')

我的src/store/modules/authToken.js文件如下所示:

const authToken = {
    state: { 
        token: localStorage.getItem('user-token') || '',
        status: '',
    },
    mutations: {
      authSuccess(state, token) {
          console.log('hellllo')
          state.token = token
          state.status = 'success'
      },

      authFail(state) {
          state.status = 'error'
      }
    },

    getters: {
      isAuthenticated: state => {
          return !!state.token
      },
      authStatus: state => {
          return state.status
      }
    }
}
import Vue from 'vue'
import Vuex from 'vuex'
import authToken from './modules/authtoken'

Vue.use(Vuex)
Vue.config.devtools = true
export const store = new Vuex.Store({
    modules: {
        authToken
    }
})
我的src/store/store.js文件如下所示:

const authToken = {
    state: { 
        token: localStorage.getItem('user-token') || '',
        status: '',
    },
    mutations: {
      authSuccess(state, token) {
          console.log('hellllo')
          state.token = token
          state.status = 'success'
      },

      authFail(state) {
          state.status = 'error'
      }
    },

    getters: {
      isAuthenticated: state => {
          return !!state.token
      },
      authStatus: state => {
          return state.status
      }
    }
}
import Vue from 'vue'
import Vuex from 'vuex'
import authToken from './modules/authtoken'

Vue.use(Vuex)
Vue.config.devtools = true
export const store = new Vuex.Store({
    modules: {
        authToken
    }
})
在main.js文件中,我使用的存储如下:

import { store } from './store/store'
new Vue({
  render: h => h(App),
  store,
  router,
}).$mount('#app')
现在,当我尝试访问组件文件中的autoken模块时,我无法访问它。我正在执行此操作。$store.state.authToken.getters.isAuthenticated

但是当我尝试使用它时,我得到了以下错误

挂载挂钩中出错:“TypeError:无法读取属性 未定义的“isAuthenticated”


这是因为您忘记在文件
src/store/modules/authToken.js
中导出对象。由于未导出任何内容,因此为存储提供的
authToken
变量将
未定义

只需在文件末尾添加以下内容:

export default authToken;

伟大的这似乎让我可以访问我的authToken状态。非常感谢。然而,我似乎仍然不能使用getter。当我使用这个时,我得到了相同的错误。$store.state.authToken.getters.isAuthenticated,但是如果我使用他的。$store.state.authToken.token,我可以访问我的令牌值。为什么会这样?当您尝试使用
this.$store.state.authToken.getters.isAuthenticated时会发生什么情况?此外,我建议您使用地图助手()。这可能有助于您获得更清晰的代码。好吧,我明白了,我显然能够通过使用此来访问我的authtoken获取程序。$store.getters.isAuthenticatedAh,这可能是因为您没有在存储选项中设置
namespaced:true
。如果答案解决了您的问题,请不要忘记将其标记为已接受,以便其他用户/读者可以更容易地看到修复。