javascript - vuex-persistedstate:如何在 vuex-persistedstate 中使用 getter?
问题描述
我使用vuex-persistedstate。但是吸气剂不起作用。我在 Vue.js Devtools 中检查了状态,状态存在。但是吸气剂没有数据。像这样。
那么如何使用吸气剂呢?
Vuex 中的 index.js
import createPersistedState from 'vuex-persistedstate'
Vue.use(Vuex)
export default new Vuex.Store({
modules: {
login,
segments,
itemList,
orderList
},
plugins: [
createPersistedState()
]
})
vue-router 中的 index.js(我想使用 getter 进行登录状态检查)
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(在 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
}
解决方案
我认为您正在访问外部state
对象。请尝试添加state
为函数参数:
const getters = {
loginStatus: (state) => state.loginStatus,
email: (state) => state.email,
isStaff: (state) => state.isStaff,
company: (state) => state.company,
token: (state) => state.token
}
推荐阅读
- sql - 加入临时表时,Visual Studio 未在 SQL Server 数据库项目中报告错误 71501
- mysql - 使用 Spring boot 和 UI 将 excel 文件内容上传到 mysql
- python - 如何读取FASTA格式文件并统计文件中的字母顺序
- mysql - MySQL/Spring:无法删除或更新外键约束失败的父行
- sql-server - spark.read 对 SQL Server 表的读取错误(通过 JDBC 连接)
- c# - 在 C# .NET Core 中访问 SignalR 性能计数器
- excel - Excel vba - 选择第一列
- javascript - REST 调用中的多个标头给出 #415 错误
- json - 将 JSON 变量文件转换为 CSV 的 Powershell 脚本
- javascript - SVG + javascript单击并拖动缓慢