vue.js - Vue router authentication-guard 无法识别我的 vuex 商店
问题描述
我正在尝试使用 来保护全局 router.js 文件中的 vue 组件beforeEnter()
,但是我在控制台中不断收到类型错误,这就是说
无法读取未定义的属性获取器
这是 router.js 文件的示例
import login from './views/login'
//the component I want to guard
import admin from './views/adminPage'
//my Vuex store
import { store } from './store'
Vue.use(Router)
export default new Router({
mode: 'history',
base: process.env.BASE_URL,
routes: [
{
path: '/login',
name: 'login',
component: login
},
{
path: '/admin'
name: 'admin',
component: admin,
beforeEnter(to, from, next){
if(!store.getters.currentUser){
next('/login')
} else {
next()
}
}
}
]
})
//this is an example of my store.js file
export default new Vuex.Store({
state: {
currentUser: null
},
//....
getters: {
currentUser: state => state.currentUser
}
})
<
注意:我相信问题出在 router.js 文件中,因为我在所有应用程序文件中调用我的 vuex getter 都没有问题。
解决方案
推荐阅读
- php - 更改数组中的数据
- java - 如何在 VScode 中制作脚本,以便当我按下某个键时它会在外部终端中运行并编译 Java 程序?
- canvas - 画布矩形模糊
- javascript - tinyMCE textpattern 插件没有按照指定的模式更改文本
- javascript - 使用 chart.js 可视化冒泡排序
- neural-network - BERT模型的参数个数是怎么计算的?
- html - Troube 使用 ngFor 正确迭代数组
- c# - 当类具有循环引用时,Swashbuckle 抛出 StackOverFlow 异常
- r - svymean 为所有变量返回 0 均值和 SE
- python - 保存下一个 programmstart tkinter 的条目值