javascript - 从 main.js 访问存储
问题描述
我正在尝试从我的 main.js 文件访问我的 vue.js 应用程序中的商店。但由于某种原因,商店是undefined
我尝试使用它的时候。这是我的代码:
main.js
import { store } from './store/store'
router.beforeEach((to, from, next) => {
if (to.meta.requiresAuth && !store.getters.isLoggedIn) {
next({ path: '/' })
} else if (to.path === '/' && store.getters.isLoggedIn) {
next({path: '/dashboard'})
} else if (store.getters.isLoggedIn && !to.meta.requiresAuth) {
next({path: '/dashboard'})
} else {
next()
store.commit('CLOSE_USER_DROPDOWN')
}
})
商店/商店.js
import Vue from 'vue'
import Vuex from 'vuex'
import auth from './modules/auth'
import optionDropdown from './modules/option_dropdown'
import userDropdown from './modules/user_dropdown'
import flash from './modules/flash'
import createPersistedState from 'vuex-persistedstate'
Vue.use(Vuex)
export const store = new Vuex.Store({
plugins: [createPersistedState({ paths: ['auth'] })],
state: {
},
getters: {
},
mutations: {
},
modules: {
auth,
flash,
userDropdown,
optionDropdown
}
})
但是当我尝试从商店读取时,它说它是未定义的。我不确定这是为什么?我正在导入商店。有人有想法吗?
解决方案
我觉得课堂上的 const 是问题所在。
在main.js
:
import store from './store/store'
在你的 store.js 中试试这个:`
export default new Vuex.Store({
plugins: [createPersistedState({ paths: ['auth'] })],
state: {
},
getters: {
},
mutations: {
},
modules: {
auth,
flash,
userDropdown,
optionDropdown
}
})
`
推荐阅读
- php - 当我尝试使用 set_error_handler 时,Laravel 出现 unserialize() 错误
- pdf - 如何在 PowerBuilder 中动态创建 PDF 文档?
- javascript - jQuery:c.replace 不是函数
- azure-active-directory - 如何获取已登录用户的 Azure AD objectid?
- pcf - PCF 在非工作时间自动删除和启动应用程序
- android - Sqlite 数据库在 Samsung Android Oreo 8.0 设备中无法正常运行
- apache-spark - 资源报价如何与 mesos 一起用于 Spark 流应用程序?
- java - 验证某些密钥是否存在于放心响应中,而不需要对响应进行 n 次迭代
- c++14 - 想知道 const char* ptr 是否会导致任何内存泄漏,
- javascript - 当我尝试一个接一个地调用我注册的协议时,只有一个可以随机调用