javascript - Vue - 组件中无法访问的商店模块
问题描述
我的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
}
}
}
我的 src/store/store.js 文件是这样的:
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
您提供给商店的变量将是undefined
.
只需在文件末尾添加:
export default authToken;
推荐阅读
- python - 在 Windows 10 上安装 TensorFlow 时出现问题
- angular - 将一个角度应用程序导航到另一个角度应用程序时量角器超时错误
- postgresql - _eq 对不同表中 ID 的权限值?
- python - 无法使用 Pyspark 在 Windows 机器上读取文本文件。(分析异常)我的文件名带有 file:// 前缀
- python - 在 django 中下载 zip 并解析 csv 文件,将数据复制到本地数据库(sqllite)
- azure-devops - 通过 Visual Studio 在本地访问 Azure Devops
- python - python中的CNN。图像数组
- ios - 有没有办法使“Apple Push Services”证书有效期有效?
- java - Spring中注解的执行顺序
- r - 无法在R中覆盖两层地图